Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function + Javascript how to get the data I pass in the post request?

I have an Azure function written in Javascript.

the functions takes as a parameter the context and the request as follows:

function(context, req)

It's pretty easy to retrieve anything passed through a GET request by using the req object, if for example I pass name=test in the URL I can retrieve it in my code as follows:

var myVar = req.query.name

Anyhow, when it happens that the verb is not a GET but a POST (and as a consequence the data are passed in the body and not as param in the URL), I don't know the way to retrieve the data.

The official documentation did not help me to understand this specific context, although it should be a silly thing.

How can I populate the variable myVar if the key "name" is passed in the body?

Any help would be appreciated

like image 525
Giuseppe Di Federico Avatar asked Jan 29 '26 23:01

Giuseppe Di Federico


2 Answers

If you POST to the Azure Function you can use require('querystring') https://nodejs.org/api/querystring.html

For example if you have an HTML page with a form with POST action e.g.:

```
<form action="..../api/{your_azure_function}">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form> 
```

Your azure function(Nodejs) will need to: /* Require for Post */ const querystring = require('querystring');

Then you can access the body with: var user_input = querystring.parse(req.body); firstn1 = user_input.firstname //contains Mickey lastn = user_input.lastname //contains Mouse

like image 83
Lico Avatar answered Feb 01 '26 14:02

Lico


I believe you would want to use context.req.body to get the request body. You can parse the body and get your name attribute.

See if this is useful: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node#http-triggers-and-bindings

like image 39
Matt Avatar answered Feb 01 '26 14:02

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!