Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(azure): raw body no longer returns parsed json #589

Merged
merged 3 commits into from
Oct 17, 2022
Merged

Conversation

tobiasdiez
Copy link
Contributor

πŸ”— Linked issue

None

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Azure functions with httpTrigger automatically JSON-parse the body. This is not explicitly mentioned in the docs (at least I couldn't find it), but in one of the examples they use req.body.name showing that req.body is indeed an object. This then leads to problems when used with h3's useRawBody method, which actually returns the object instead of the stringy body when deployed to azure.

This is fixed by using req.rawBody in the azure handler. It's hard to find documentation on this property, but rawBody is used in an official test file:
https://github.com/Azure/azure-functions-host/blob/71ecbb2c303214f96d7e17310681fd717180bdbb/test/WebJobs.Script.Tests.Integration/TestScripts/Node/HttpTrigger/index.js#L13

Example console.log(req) in the handler:

....
[api] [2022-10-16T13:33:53.998Z]   body: {
[api] [2022-10-16T13:33:53.998Z]     query: 'query MeE2ENotLoggedIn {\n  me {\n    id\n  }\n}',
[api] [2022-10-16T13:33:53.998Z]     operationName: 'MeE2ENotLoggedIn'
[api] [2022-10-16T13:33:53.998Z]   },
[api] [2022-10-16T13:33:53.998Z]   rawBody: '{"query":"query MeE2ENotLoggedIn {\\n  me {\\n    id\\n  }\\n}","operationName":"MeE2ENotLoggedIn"}'
....

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Oct 16, 2022

Codecov Report

Merging #589 (216cd9b) into main (6d4b416) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #589   +/-   ##
=======================================
  Coverage   66.09%   66.09%           
=======================================
  Files          55       55           
  Lines        4109     4109           
  Branches      444      444           
=======================================
  Hits         2716     2716           
  Misses       1389     1389           
  Partials        4        4           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding! ❀️

@pi0 pi0 merged commit 5c2c313 into unjs:main Oct 17, 2022
@tobiasdiez tobiasdiez deleted the patch-1 branch October 17, 2022 14:33
tobiasdiez added a commit to JabRef/JabRefOnline that referenced this pull request Oct 19, 2022
@brendonmatos
Copy link
Contributor

@danielroe Hi!
Should useRawBody always return the body in string?

if yes, the same is happening in cloudflare deploys

WinterYukky pushed a commit to WinterYukky/nitro that referenced this pull request Nov 1, 2022
Co-authored-by: Daniel Roe <daniel@roe.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants