Giter VIP home page Giter VIP logo

Comments (7)

ejizba avatar ejizba commented on June 22, 2024 4

Try removing these lines:

// debug only to see what came in
const allText = await req.text();
context.log(allText)

The request body functions can be run only once; subsequent calls will resolve with empty strings/ArrayBuffers.

from azure-functions-nodejs-library.

arlogilbert avatar arlogilbert commented on June 22, 2024 2

What is the rationale behind making the body only available for a single request?

There are plenty of times where we need to access the raw body to do things such as validate signatures from third parties for security purposes, those generally need the rawbody (v3).

Then separately we want to do crazy things like pull keys out of the JSON that was sent to us. Currently our solution is to do a JSON.parse() on the text output, but somehow hash of the raw body is different than the hash of the parsed JSON, even though it is well formed.

Love the v4 model, but wow there are some big differences that are making life harder.

from azure-functions-nodejs-library.

diberry avatar diberry commented on June 22, 2024

That works for me. Thanks.

from azure-functions-nodejs-library.

ejizba avatar ejizba commented on June 22, 2024

Hi @arlogilbert, good questions. First of all, we're leveraging a design by the much larger Node.js community called the fetch standard (some docs here). In the v4 model we wanted to leverage an existing design for http types instead of using one unique to Azure Functions. Other folks have asked your same question to the larger community, and here is one example.

AFAIK, the primary reason is to allow the use of streams under the covers and by definition streams can only be read once. Using a stream has big performance and throughput benefits, so we've been preparing for that for a while. If you want to get into the nitty gritty, the functions implementation doesn't technically use a stream yet, but it will very soon and moving to these community-designed types was a first step in the process.

I believe you're already following the recommended workaround, which is to call .text(), save that value, and then call JSON.parse on it when needed. It's not clear to me why that doesn't work for you, but if you elaborate with sample data and sample code I might be able to help.

from azure-functions-nodejs-library.

arlogilbert avatar arlogilbert commented on June 22, 2024

from azure-functions-nodejs-library.

restfulhead avatar restfulhead commented on June 22, 2024

@ejizba I understand the design choice and looking forward to stream support. However, it would be beneficial to have an option to cache the request body. I'm writing a hook that validates the request body. The hook consumes it using .json(). Now the function has no way to access the request body. Because everything is readonly in HttpRequest I also don't see an easy option to reset/replace the consumed stream. Can you provide guidance how a preInvocation hook and a function can use the request body?

Update: I've tried to use pipeThrough, but that still causes the bodyUsed flag to be true. Looks like the only option is to copy the entire request:

  const textBody = await request.text()
  parsedBody = JSON.parse(textBody)

  // TODO: validate body here

  const headerCopy: Record<string, string> = {}
  origRequest.headers?.forEach((value, key) => {
    headerCopy[key] = value
  })

  const queryParams: Record<string, string> = {}
  origRequest.query?.forEach((value, key) => {
    queryParams[key] = value
  })

  request = new HttpRequest({
    method: origRequest.method,
    url: origRequest.url,
    body: { string: textBody },
    headers: headerCopy,
    query: queryParams,
    params: origRequest.params,
  })

from azure-functions-nodejs-library.

ejizba avatar ejizba commented on June 22, 2024

@restfulhead moved your comment to a new issue: #207

from azure-functions-nodejs-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.