ofetch
ofetch copied to clipboard
post request header as 'application/x-www-form-urlencoded', request body just like to json?
Environment
"nuxt": "3.13.2", "ofetch": "1.4.0"
Reproduction
no link
Describe the bug
I use Nuxt3 framework, When I make a request
request header content-type:application/x-www-form-urlencoded
$fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
Before 1.4.0 (use v1.3.4):
click view source
cvId=4636bb5e6d50441a1nd729W0FVFZxI6-Vfqe&cvName=resume-q
Now: use 1.4.0
click view source:
{"cvId":"eafxxxE1NTxIi3V_4~","cvName":"111"}
It should be that ofetch was also updated when nuxt was updated.
Now I set the version of ofetch to 1.3.4 and everything works fine
@pi0 I checked the update log of the new version, and there is no mention of the changes here. Could you please check it, thank you very much.
Additional context
No response
Logs
No response
I confirm !
I tried your example, it works fine. Could you please confirm it with this minimal reproduction?
Feel free to reopen this issue if you have any futhur questions ❤️
I tried your example, it works fine. Could you please confirm it with this minimal reproduction?
Feel free to reopen this issue if you have any futhur questions ❤️
![]()
@kricsleo Thank you very much for your reply. I wrote a minimal reproducible demo. The address is here. The focus is on options.headers['Content-Type'] After upgrading to v1.4.1, the value obtained is undefined, resulting in the judgment condition not being met.
https://stackblitz.com/edit/github-wn3areuy-4v8wapun?file=index.js&startScript=start
The picture below is my test. You can try switching versions.
I tried your example, it works fine. Could you please confirm it with this minimal reproduction? Feel free to reopen this issue if you have any futhur questions ❤️
@kricsleo Thank you very much for your reply. I wrote a minimal reproducible demo. The address is here. The focus is on options.headers['Content-Type'] After upgrading to v1.4.1, the value obtained is undefined, resulting in the judgment condition not being met.
https://stackblitz.com/edit/github-wn3areuy-4v8wapun?file=index.js&startScript=start
The picture below is my test. You can try switching versions.
I found a solution.
In v1.3.4, options.headers is a normal JavaScript object, so you can access properties through options.headers['Content-Type'].
In v1.4.0+, options.headers is standardized as a Headers object, which does not support direct access to properties through the [] operator, and must use the get() method.
v1.3.4 options.headers['Content-Type']
v1.4.0 options.headers.get('Content-Type')
Write a general method that does not need to pay attention to ofetch version. Whether headers is a normal object (v1.3.4) or a Headers instance (v1.4.0+), it can get the value correctly.
function onRequest({ request, options }) {
const getHeader = (headers, name) => {
if (headers instanceof Headers) {
return headers.get(name);
}
return headers[name];
};
const contentType = getHeader(options.headers, 'Content-Type');
console.log(options.headers, contentType);
if (
contentType === 'application/x-www-form-urlencoded' &&
(options.method || '').toLowerCase() === 'post'
) {
options.body = qs.stringify(options.body);
}
console.log(options.body);
}
@cfab You can refer to it