node.js - Azure Function + Javascript how to get the data I pass in the post request? - Stack Overflow

I have an Azure function written in Javascript.the functions takes as a parameter the context and the r

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

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

Share Improve this question asked Jan 25, 2019 at 16:37 Giuseppe Di FedericoGiuseppe Di Federico 3,6094 gold badges22 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If you POST to the Azure Function you can use require('querystring') https://nodejs/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

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./en-us/azure/azure-functions/functions-reference-node#http-triggers-and-bindings

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464546a4628858.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信