0

In nodejs azure function:

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);

I am testing in the azure portal: Test body:

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

And the printed message:

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

Say I have a complex object in the request body that contains the data to be processed by my worker function, how can I read the object from the request body in this azure function?

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71

1 Answers1

1

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

The issue is that you were logging a ReadableStream object instead of the actual content of the JSON object in the request body.

  • You were logging the request.body directly, which is a ReadableStream object. This stream needs to be read and processed to get the actual content.

  • Check the below code that you can be able to read and process the JSON object from the request body.

Updated code:

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",
    };
  }

Deployment status:

enter image description here

Navigate to the function app in portal < HttpTrigger function.

enter image description here

Response:

enter image description here

Suresh Chikkam
  • 623
  • 2
  • 2
  • 6