How to use req.get_body() in azure function in python and what is purpose?

Viewed 4566

I am new in Azure function. I'm unable to understand what is the purpose of req.get_body() in azure function?? Can we pass binary image content in local host api(e.g-http://localhost:7071/api/HttpTrigger1) by using req.get_body()?? If yes, Please tell me how to pass binary image content in local host. I'm trying it with name = req.params.get('name'). But problem is that if image content is small then i can pass successfully but if image content is large it show error.

Please help me out

2 Answers

Yes, as you mentioned in question, "Get" method request can just be used with small request parameter. If your image content is large, we'd better to use "Post" method to request the function and put the binary in the request body of "Post" request. Apart from this, "Post" method is more security than "Get" method request.

When you test in local, you just need to use "Postman" to do it. Download "Postman" and do it like below screenshot:

enter image description here

The purpose of req.get_body() is to retrieve the data from the post request that is made.

For example, if you make a get request to your azure function at http://localhost:7071/api/HttpTrigger, and you write { "message" :" Success" }

And if you did something similar to this in your function:

import json

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

  
    message = req.get_body()
   
   
 
    return func.HttpResponse(f"Received the message: {message}", status_code=200)

You would receive as a reply the message "SUCCESS" which was sent by Postman or similar.

Related