question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useBody with `application/x-www-form-urlencoded`

See original GitHub issue

Hello, when using useBody I get a string instead of an object. Something like this field=value&another=value. Is there an option to make this an object? What I currently do is this:

let body = await useBody(req)
body = Object.fromEntries(new URLSearchParams(body))

thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AustinGilcommented, Dec 13, 2022

Have confirmed that the current solution causes data loss.

When you submit this form:

<form>
  <input name="name" value="1" />
  <input name="name" value="2" />
  <button>Submit</button>
</form>

await readBody(event) returns this data

{
  name: '2'
}

The expected data should be something like:

{
  name: ['1', '2']
}

This can be accomplished with the qs package:

let body
if (headers['content-type']?.includes('application/x-www-form-urlencoded')) {
  body = qs.parse(await readRawBody(event));
}
1reaction
NozomuIkutacommented, Aug 6, 2022

@pi0

As @walmartwarlord said, you already released this feature as a part of v0.5.3.

https://github.com/unjs/h3/blob/v0.5.3/CHANGELOG.md#053-2022-03-31

So you could close this issue, or are there any remaining issues in your mind?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send post request with x-www-form-urlencoded body
As you set application/x-www-form-urlencoded as content type so data sent must ... Building off of Navoneel's answer, I like to use StreamEx's EntryStream....
Read more >
'x-www-form-urlencoded' or 'form-data' ? Explained in 2 mins.
For x-www-form-urlencoded , use bodyParser, which will parse payload into req.body in the format of { key, value } .
Read more >
Difference Between form-data, x-www-form-urlencoded and ...
In this tutorial, we'll explore the differences between using form-data, x-www-form-urlencoded, and raw for our request body.
Read more >
POST - HTTP - MDN Web Docs
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header....
Read more >
How to send x-www-form-urlencoded body?
Hi, Instead of JSON, how to send the data in the body by x-www-form-urlencoded? I mean the format is: name=maggie instead of.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found