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.

How to remove 'text/plain;charset=UTF-8' from content-type header?

See original GitHub issue

Hi, I am trying to send a post request to PostgREST to invoke a stored procedure:

  const data = await $fetch('http://http-logger:3001/rpc/test_proc', {
    method: 'post',
    headers: {
      Prefer: 'params=single-object',
      Accept: 'application/json',
      'Content-Type':  'application/json'
    },
    body: {some: 'json'}
})

Results in this request:

POST /rpc/test_proc HTTP/1.1
host: http-logger:3001
connection: keep-alive
prefer: params=single-object
accept: application/json
content-type: application/json, text/plain;charset=UTF-8
accept-language: *
sec-fetch-mode: cors
user-agent: undici
accept-encoding: gzip, deflate
content-length: 15


{"some":"json"}

I need to remove the ‘text/plain;charset=UTF-8’ part from the content-type. Is this possible? Should it even be there as I have explicitly defined the content-type header? Many thanks Ian

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
phorcys420commented, Aug 3, 2022

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.

1reaction
phorcys420commented, Aug 3, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

HttpClient: How to Remove Charset From Content-Type Header
Removing the Charset From a Content-Type Header. Solution is simple - we can create StringContent with whatever encoding and then set charset to ......
Read more >
HttpClient: How to remove charset from ... - Gunnar Peipman
Removing charset from content-type header. Solution is simple – we can create StringContent with whatever encoding and then set charset to empty ...
Read more >
HTTP request, content-type missing - Stack Overflow
But, when I remove the content-type, the server receives "text/plain;charset=UTF8". Why is my content-type header not being sent?
Read more >
Managing Messages - Amazon Lex - AWS Documentation
When you use a message group to group messages by type, don't use the x-amz-lex:accept-content-types header. For more information about the x-amz-lex:accept- ...
Read more >
How to remove header.Content-type? - Google Cloud Community
So my proxy endpoint asks for a json payload. And the user will call the proxy by supplying a json payload and 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