admin管理员组文章数量:1426224
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 badges2 Answers
Reset to default 2If 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
本文标签: nodejsAzure FunctionJavascript how to get the data I pass in the post requestStack Overflow
版权声明:本文标题:node.js - Azure Function + Javascript how to get the data I pass in the post request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745464546a2659474.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论