0

I am using Python. Have to pass a variable (comName)in Azure function to the return func.HttpResponse . below is my code.

    comName= 'Xerox'
    return func.HttpResponse(status_code=200,headers={'content-type':'text/html'}, 
            body=
                """<!DOCTYPE html>
                <html>
                <body>    
                <h1>Hello , thanks for your response {name} </h1>
                </body>
                </html>
                """
                )

this is working and return the h1 tag as its. Hello , thanks for your response {name}

Thanks.

3
  • 1
    not really good at python coupled with azure functions, but my basic query is, did u try formatting options for strings in python? some are listed here- realpython.com/python-string-formatting/… Commented Oct 25, 2020 at 12:05
  • Thanks. Yes, I tried this and working fine.. I want HTML as response back to end users . there are near about 10 other HTML tags that I am using and working fine except passing the variable.. in string format, things are fine except response back to the user is not intuitive. this return as simple string line of code with variables without any formatting. Commented Oct 25, 2020 at 21:43
  • Hi, you can use f-string to get the variable, this feature is start from python 3.7. This is the doc: docs.python.org/3.7/tutorial/inputoutput.html You can have a look of my answer, it is a simple example. Commented Oct 26, 2020 at 2:22

1 Answer 1

3

Yes, of course you can.

Have a look of below simple example:

import logging

import azure.functions as func
import sys

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    name = 'Bowman'
    return func.HttpResponse(
            body = f"<!DOCTYPE html><html><body><h1>Hello , thanks for your response {name} </h1></body></html>",
            headers={'content-type':'text/html'},
            status_code=200
    )

Use f-string to get the variable.

This is a feature from python 3.7. previous versions need to use format() method. This is the offcial doc:

https://docs.python.org/3.7/tutorial/inputoutput.html

I can get the response:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your time. I am getting out put ....<!DOCTYPE html><html><body><h1>Hello , thanks for your response Bowman </h1></body></html>.. I want output --Hello , thanks for your response Bowman
@Andrew Does this achieve that you need?
No... as mentioned, i am getting whole string as out out... variable name is coming but out put is .... <!DOCTYPE html><html><body><h1>Hello , thanks for your response Bowman </h1></body></html>... Basically this is same as of ..(f:Hello , thanks for your response {name} .. I want my html tags inside and tags should render in browser.
@Andrew I have update. I forget to use headers={'content-type':'text/html'} before...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.