简体   繁体   English

Azure Function + Javascript 如何获取我在 post 请求中传递的数据?

[英]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.

相关问题 如何将GET响应数据传递到POST请求 - How to pass GET response data to POST Request 如何将Android发布请求响应传递给javascript函数? - How to pass Android post request response to javascript function? 如何在 JavaScript 中的 POST 请求 URL 中传递变量? - How do I pass a variable inside a POST request URL in JavaScript? 如何将数据从$ .post请求传递到文件并打印 - How can I pass data from $.post request to a file and print 如何将这个AJAX请求函数(原始Javascript)转换为使用POST而不是GET? - How can I convert this AJAX request function (raw Javascript) to use POST instead of GET? 如何使用Javascript的提取获取POST请求数据? - How to Use Javascript's Fetch to get POST Request Data? 如何在javascript中获取POST请求的大小? - How can I get the size of a POST request in javascript? 我已经从我的jsp服务器中的android应用收到了HTTP POST,如何将数据传递到Javascript函数中 - I've received an HTTP POST from an android app in my jsp server, how do I pass that data into a Javascript function 如何将我从 JavaScript 中的提取请求中获得的数据保存到我的 function 外部的变量中 - How do I save the data I get from a fetch request in JavaScript into a variable that is external to my function 如何将Vue数据传递到Axios发布请求中? - How to Pass Vue data into Axios Post Request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM