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

Impossible to send x-www-form-urlencoded payload the fetch way #33

Closed
Aareksio opened this issue Nov 29, 2021 · 1 comment
Closed

Impossible to send x-www-form-urlencoded payload the fetch way #33

Aareksio opened this issue Nov 29, 2021 · 1 comment

Comments

@Aareksio
Copy link

Aareksio commented Nov 29, 2021

Hi! The following requests fails to send x-www-form-urlencoded payload:

import { URLSearchParams } from 'url';
import { $fetch, fetch } from 'ohmyfetch';

(async function main() {
  const body = new URLSearchParams({ test: 'true' });

  const responseA = await $fetch('https://httpbin.org/post', { method: 'POST', body });
  console.log(responseA);

  const responseB = await fetch('https://httpbin.org/post', { method: 'POST', body });
  console.log(await responseB.json());
})();
// $fetch
{
  args: {},
  data: '{}',
  files: {},
  form: {},
  headers: {
    Accept: 'application/json',
    'Accept-Encoding': 'gzip,deflate,br',
    'Content-Length': '2',
    'Content-Type': 'application/json',
    Host: 'httpbin.org',
    'User-Agent': 'node-fetch',
    'X-Amzn-Trace-Id': 'Root=1-61a56083-3dd9c60f2746207123123123'
  },
  json: {},
  origin: '123.123.123.123',
  url: 'https://httpbin.org/post'
}
// fetch
{
  args: {},
  data: '',
  files: {},
  form: { test: 'true' },
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate,br',
    'Content-Length': '9',
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    Host: 'httpbin.org',
    'User-Agent': 'node-fetch',
    'X-Amzn-Trace-Id': 'Root=1-61a56084-557df690674d50c123123123'
  },
  json: null,
  origin: '123.123.123.123',
  url: 'https://httpbin.org/post'
}

Why is this important? Fetch spec allows for passing URLSearchParams to indicate x-www-form-urlencoded payload. node-fetch follows the spec. ohmyfetch does not, despite sending the user to node-fetch documentation for references.

Why it fails? Probably typeof (new URLSearchParams) === 'object' here

@pi0
Copy link
Member

pi0 commented Dec 14, 2021

Hi @Aareksio. This issue should be fixed on 0.4.9 (4adb3bc)

@pi0 pi0 closed this as completed Dec 14, 2021
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

No branches or pull requests

2 participants