How to return HTML and JS file in HTTP Response (Azure-Functions)?

Yu Kawasaki 1 Reputation point
2022-03-05T08:02:59.883+00:00

Hello
I use Azure Function(python)
I wanna return html and js file in http response

I try this code and i return only html file

   import logging  
     
   import azure.functions as func  
     
     
   def main(req: func.HttpRequest) -> func.HttpResponse:  
       filename = f'HttpTrigger1/test.html'  
       with open(filename, 'rb') as f:  
           return func.HttpResponse(  
               body=f.read(),  
               mimetype='text/html',  
               status_code=200  
           )  

But Http response doesn't found js file
I also wanna return js file

My File structure is below

How to return HTML and JS file in HTTP Response (Azure-Functions)?

Please tell me!!

180313-cap.png

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,353 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 20,431 Reputation points
    2022-03-07T12:03:03.843+00:00

    Hi @Yu Kawasaki

    Welcome to Microsoft Q&A! Thanks for posting the question.

    I see that you are trying to return content of 2 files (a html doc and a script file) as a response from Python Azure Function. Please note the signature of azure.functions.HttpResponse constructor as available here: HttpResponse Class:

    HttpResponse(body=None, *, status_code=None, headers=None, mimetype=None, charset=None)  
    

    As a response, you may only return one value which is the body (). In the code snippet as shared above, you are only reading the html file (filename = f'HttpTrigger1/test.html') as text. This will only get the content of file itself and not the dependent resource (here test.js). You may want to read the content from both the file individually, append the content into a single string and then return it back as a string.

    Please let me know if you have any questions.

    ---
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.