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.

Content-Type not working at GET requests

See original GitHub issue

I’m trying to make a GET request with a ‘Content-Type’ like ‘application/vnd.api+json’ and its not working, the content-type is not being changed. 😢

const api = create({
  baseURL: 'http://localhost:3000',
  headers: {
    'Content-Type': 'application/vnd.api+json',
    'Accept': 'v1'
  }
})

api
  .get('users/token_exists', { confirmation_token: token })
  .then((response) => {
    console.log(response.status)
  })

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
buneacommented, Jul 10, 2018

@fabiodiniz This isn’t actually a bug in either of the libraries though. The Content-Type header refers to the type of data you are sending to the server and as such there is no reason to send it if there’s no body to the request. My guess is that you would want to use the Accept header which specifies how you want your response formatted.

This is what I think you’re actually looking for:

const api = create({
  baseURL: 'http://localhost:3000',
  headers: {
    'Accept': 'application/vnd.api+json'
  }
})

api
  .get('users/token_exists', { confirmation_token: token })
  .then((response) => {
    console.log(response.status)
  })
3reactions
fabiodinizcommented, Jun 22, 2018

@guziyu001

You need to pass an empty data object, so the Content-Type will be sent . Idk why, but Axios don’t send custom headers in GET requests when there is no data object.

Your code would look like this:

api.get('', null, {
  data: {},
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Do I need a content-type header for HTTP GET requests?
GET requests can have "Accept" headers, which say which types of content the client understands. The server can ...
Read more >
Content-Type - HTTP - MDN Web Docs - Mozilla
In responses, a Content-Type header provides the client with the actual content type of the returned content. This header's value may be ignored...
Read more >
HTTP request with Content-type not supported
Hello,. I created a flow in Power Automate, and when I run a specific HTTPS request (GET method) with the HTTP module :....
Read more >
How do I set the content type for a Curl request? - ReqBin
Setting the Content-Type with Curl Request. To send the Content-Type header using Curl, you need to use the -H command-line option.
Read more >
Quickstart — Requests 2.28.1 documentation
Requests ' simple API means that all forms of HTTP request are as obvious. ... Please note that the above code will NOT...
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