ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

post request header as 'application/x-www-form-urlencoded', request body just like to json?

Open rui-ys opened this issue 1 year ago • 1 comments

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):

image

click view source

cvId=4636bb5e6d50441a1nd729W0FVFZxI6-Vfqe&cvName=resume-q

Now: use 1.4.0

image

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

rui-ys avatar Sep 24 '24 05:09 rui-ys

I confirm !

cfab avatar Sep 24 '24 08:09 cfab

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 ❤️

Image

kricsleo avatar Mar 27 '25 13:03 kricsleo

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 ❤️

Image

@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.

Image

rui-ys avatar Jun 25 '25 13:06 rui-ys

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 ❤️ Image

@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.

Image

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);
}

Image

@cfab You can refer to it

rui-ys avatar Jun 27 '25 08:06 rui-ys