Comments (7)
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.
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.
That works for me. Thanks.
from azure-functions-nodejs-library.
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.
from azure-functions-nodejs-library.
@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.
@restfulhead moved your comment to a new issue: #207
from azure-functions-nodejs-library.
Related Issues (20)
- Allow users to specify IncludeEmptyEntriesInMessagePayload capability HOT 4
- Support warmup trigger
- EventGridOutput doesn't contain all types for typescript HOT 1
- app.storageBlob extraOutput w/ queue binding not working HOT 6
- EnableWorkerIndexing flag still required when using Dockerfile HOT 3
- Fix uncaught exception link for v4 model
- Error with v4 EventHub trigger and cardinality many HOT 5
- HttpTriggerOptions default AuthLevel is inconsistent with Functions documentation HOT 1
- Cannot debug v4 on VS Code HOT 3
- Context Logging not in Sequential order HOT 7
- Unclear how to configure `librdkafka` settings in JS HOT 3
- Node.js v4 pm: Incorrect route triggered HOT 6
- 1 Function app - 2 different auths? HOT 2
- "@azure/functions-core" can't be resolved by bundlers HOT 5
- Turn on strict rules for samples repo HOT 5
- Exception: Cannot set property message of [object Object] which has only a getter HOT 3
- Issue with Azure Functions v4 FormData Handling for Large field JSON Data HOT 4
- How to reference HttpRequest body in hooks HOT 4
- Document hooks feature HOT 1
- Cannot read properties of undefined (reading 'type') HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-functions-nodejs-library.