gpt4 book ai didi

Azure函数nodejs : How can I extract the object from the request body in this azure function?

转载 作者:行者123 更新时间:2023-12-02 08:11:13 24 4
gpt4 key购买 nike

在nodejs azure函数中:

import {
app,
HttpRequest,
HttpResponseInit,
InvocationContext,
} from "@azure/functions";

export async function workerfunction(
request: HttpRequest,
context: InvocationContext
): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
context.log("request.body: ", request.body);

我正在 azure 门户中测试:测试体:

{"body":"this is a test message"}

以及打印的消息:

2023-08-02T06:49:19Z   [Information]   request.body:  ReadableStream { locked: true, state: 'closed', supportsBYOB: false }

假设我的请求正文中有一个复杂的对象,其中包含要由我的辅助函数处理的数据,我如何从这个 azure 函数的请求正文中读取该对象?

最佳答案

2023-08-02T06:49:19Z [Information] request.body: ReadableStream { locked: true, state: 'closed', supportsBYOB: false }

问题在于您记录的是 ReadableStream 对象,而不是请求正文中 JSON 对象的实际内容。

  • 您直接记录 request.body,它是一个 ReadableStream 对象。需要读取并处理该流才能获取实际内容。

  • 检查以下代码,您可以从请求正文中读取并处理 JSON 对象。

更新的代码:

import {
app,
HttpRequest,
HttpResponseInit,
InvocationContext,
} from "@azure/functions";

export async function workerfunction(
context: InvocationContext,
request: HttpRequest
): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);

try {
const requestBody = request.body;

context.log("request.body: ", requestBody);

// Your processing logic using requestBody

return {
status: 200,
body: "Request processed successfully",
};
} catch (error) {
context.log.error("Error processing request:", error);

return {
status: 500,
body: "Internal server error",
};
}

部署状态:

enter image description here

导航到门户中的函数应用

enter image description here

回应:

enter image description here

关于Azure函数nodejs : How can I extract the object from the request body in this azure function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76817402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com