[英]Azure Function + Javascript how to get the data I pass in the post request?
I have an Azure function written in Javascript.我有一个用 Javascript 编写的 Azure 函数。
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:使用 req 对象检索通过 GET 请求传递的任何内容非常容易,例如,如果我在 URL 中传递 name=test 我可以在我的代码中检索它,如下所示:
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.无论如何,当动词不是 GET 而是 POST 时(因此数据在正文中传递而不是作为 URL 中的参数),我不知道检索数据的方法。
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?如果键“名称”在主体中传递,我如何填充变量 myVar?
Any help would be appreciated任何帮助,将不胜感激
I believe you would want to use context.req.body to get the request body.我相信您会想要使用 context.req.body 来获取请求正文。 You can parse the body and get your name attribute.
您可以解析正文并获取您的 name 属性。
See if this is useful: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#http-triggers-and-bindings看看这是否有用: https : //docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#http-triggers-and-bindings
If you POST to the Azure Function you can use require('querystring') https://nodejs.org/api/querystring.html如果您发布到 Azure 函数,您可以使用 require('querystring') https://nodejs.org/api/querystring.html
For example if you have an HTML page with a form with POST action eg:例如,如果您有一个带有 POST 操作表单的 HTML 页面,例如:
```
<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');
您的 azure 函数(Nodejs)将需要:
/* 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
然后你可以访问主体:
var user_input = querystring.parse(req.body); firstn1 = user_input.firstname //contains Mickey lastn = user_input.lastname //contains Mouse
var user_input = querystring.parse(req.body); firstn1 = user_input.firstname //contains Mickey lastn = user_input.lastname //contains Mouse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.