我有带有 Http 触发器的 TypeScript 天蓝色函数。我正在使用 POST 方法并将正文发送到 azure 函数。但我无法阅读,请求正文数据作为 Javascript 对象。
我的功能代码
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
context.log('HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
if (name) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "Ar Item search " + (req.query.name || req.body.name)
};
}
else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
};
export default httpTrigger;
由于上面的图像正文不是普通的 http post 请求正文的 Json 对象。它是一个字符串
name=Janith&age=25 我无法阅读
req.body.name
示例代码。我需要它读作
{
"name":"Janith",
"age":25
}
我的function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/ARItemSearch/index.js"
}