Giter VIP home page Giter VIP logo

Comments (7)

phorcys420 avatar phorcys420 commented on August 10, 2024 1

I can confirm that this is only an issue with undici fetch (globalThis.fetch)

from ofetch.

phorcys420 avatar phorcys420 commented on August 10, 2024 1

Actually, it is only an issue with undici fetch in ohmyfetch context, here's a snippet to test it out :

import http from "node:http";

import { $fetch } from "ohmyfetch/node";

const server = http.createServer((req, res) => {
   console.log(req.headers);

  res.end();
});

server.listen(8000);

for(const fetchImpl of [fetch, $fetch]) {
	await fetchImpl("http://localhost:8000", {
		body: { lo: "dibidon" },

		headers: {
			"content-type": "application/json"
		},

		method: "POST"
	});
}

globalThis.fetch:

{
  host: 'localhost:8000',
  connection: 'keep-alive',
  'content-type': 'application/json',
  accept: '*/*',
  'accept-language': '*',
  'sec-fetch-mode': 'cors',
  'user-agent': 'undici',
  'accept-encoding': 'gzip, deflate',
  'content-length': '0'
}

ohmyfetch:

{
  host: 'localhost:8000',
  connection: 'keep-alive',
  'content-type': 'application/json, text/plain;charset=UTF-8',
  accept: 'application/json',
  'accept-language': '*',
  'sec-fetch-mode': 'cors',
  'user-agent': 'undici',
  'accept-encoding': 'gzip, deflate',
  'content-length': '16'
}

I tried on node 16 and I had no issue with node-fetch.
So, it's an issue with ohmyfetch's implementation of undici fetching. It doesn't happen with unjs/node-fetch-native alone.
I also realized it doesn't happen if you omit the body.

from ofetch.

NozomuIkuta avatar NozomuIkuta commented on August 10, 2024 1

Let me close this issue because ohfetch does no longer support undici as of v1.0.0 and there is a workaround for the issue like using other fetch implementation.

from ofetch.

mvdschee avatar mvdschee commented on August 10, 2024

@ian4uia You have made a typo, headers are case sensitive, you can overwrite content-type with lowercase instead of capitalised Content-Type

from ofetch.

phorcys420 avatar phorcys420 commented on August 10, 2024

You have made a typo, headers are case sensitive, you can overwrite content-type with lowercase instead of capitalised Content-Type

it's not a typo, it's supposed to work too, overwriting using content-type does not work better

return $fetch("https://whatever", { 
	body: {},

	headers: {
		"content-type": "application/json"
	},

	method: "POST"
});

Result:

Content-Type: application/json, text/plain;charset=UTF-8

I'm thinking that this is potentially an undici issue

from ofetch.

mvdschee avatar mvdschee commented on August 10, 2024

Could that it's always adding the text/plain;charset=UTF-8 on any content-type which would be not very helpful.

But what I saw in fetch.js #L121 if you wanted to overwrite it you needs to use content-type instead of Content-Type

from ofetch.

phorcys420 avatar phorcys420 commented on August 10, 2024

I'm pretty sure that works for ohmyfetch, but it's probably an issue with the underlying fetch libraries used (for my case, it's using undici.fetch)

from ofetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.