axios icon indicating copy to clipboard operation
axios copied to clipboard

Setting header: 'Content-Type': 'application/json' is not working

Open alejandronanez opened this issue 11 years ago • 30 comments

Hi. I'm passing a custom header like this

axios.get('my/url', {
    headers: {
        'Content-Type': 'application/json'
    }
})

But it doesn't seem to work. Any ideas?

alejandronanez avatar Jul 13 '15 23:07 alejandronanez

There was an issue with Content-Type having to be cased correctly (see #89). I just fixed it a few minutes ago. Maybe this was the same issue you were experiencing?

mzabriskie avatar Jul 23 '15 16:07 mzabriskie

Well, I wasn't able to send the header 'Content-type' at all on my request. @mzabriskie let me give it a try and I'll let you know.

Thanks.

alejandronanez avatar Jul 23 '15 17:07 alejandronanez

This doesn't appear to be working for me, either. It looks like perhaps it's this line here? https://github.com/mzabriskie/axios/blob/master/lib/adapters/xhr.js#L77

mcmire avatar Aug 18 '15 22:08 mcmire

Is your request body empty by any chance? For me manually passing data: null to axios, in order to manually set an empty body, prevented IE from sending undefined as body.

aprzywara avatar Aug 31 '15 18:08 aprzywara

Yeah, the request body was empty.

On closer inspection, it appears that Axios is doing the right thing here. Content-Type only applies to the request body, so if it's missing then it shouldn't pass along that header.

mcmire avatar Aug 31 '15 21:08 mcmire

Yes it is correct to remove the headers, you need to pass on an empty body though (IE does not do that, it passes "undefined" which triggers errors in some/most backends).

We are using Restler in a project to serve JSON and Restler was throwing 403 errors because the "undefined" string gets passed as Content-Type: text/plain which doesn't work.

aprzywara avatar Aug 31 '15 21:08 aprzywara

Yep, I can confirm. If there is no data passed it removes the Content-Type... I just added data: {} to my GET request, and it works fine. It's really weird to pass data on get requests but I don't mind :+1:

dinodsaurus avatar Sep 11 '15 19:09 dinodsaurus

I'd vote for this issue to be closed, then. @alejandronanez what do you think?

mcmire avatar Sep 11 '15 20:09 mcmire

This stills an issue. You need to specify the data:null or data:{} to get it to work :-)

teleyinex avatar Jun 28 '17 09:06 teleyinex

@teleyinex Content-Type describes what format the request data is in. If there is no request data, there is no need to specify the Content-Type. So you can correct me if I'm wrong but I believe the present behavior makes sense, aside from the fact that it should still not add the header if data is null. What do you think?

mcmire avatar Jun 28 '17 20:06 mcmire

@mcmire then I agree completely. But, it might be good to add this to the docs, so others can easily find it.

teleyinex avatar Jun 29 '17 07:06 teleyinex

Yeah, agreed about that, it's definitely something worth documenting.

mcmire avatar Jun 29 '17 16:06 mcmire

One example in the README will do the trick :D

teleyinex avatar Jun 30 '17 07:06 teleyinex

SignIn = () => {
    console.log('login clicked')
    let data = JSON.stringify({
        password: this.state.password,
        username: this.state.email
    })

    axios.post('url', data, {
        headers: {
            'Content-Type': 'application/json',
        }
    }
    )
}

BenAhlander avatar Oct 20 '17 18:10 BenAhlander

Just for other people Googling, this is how I had to format my axios.post request

    var postData = {
      email: "[email protected]",
      password: "password"
    };

    let axiosConfig = {
      headers: {
          'Content-Type': 'application/json;charset=UTF-8',
          "Access-Control-Allow-Origin": "*",
      }
    };

    axios.post('http://<host>:<port>/<path>', postData, axiosConfig)
    .then((res) => {
      console.log("RESPONSE RECEIVED: ", res);
    })
    .catch((err) => {
      console.log("AXIOS ERROR: ", err);
    })

mdrideout avatar Jan 27 '18 19:01 mdrideout

Is there a particular reason to remove content-type in this line if data is not provided? https://github.com/axios/axios/blob/master/lib/adapters/xhr.js#L135 Why my get request can't have content-type: application/json? This is super strange to write everywhere

await axios.get(url, {data: null})

EJIqpEP avatar May 15 '18 20:05 EJIqpEP

@EJIqpEP check what @mcmire said.. so basically you need to handle this on your backend

dinodsaurus avatar May 16 '18 05:05 dinodsaurus

Thanks for clarifying @dinodsaurus.

EJIqpEP avatar May 16 '18 12:05 EJIqpEP

const config = {
  headers: {
    accept: 'application/json',
  },
  data: {},
};

Same issue successfully fixed with data: {}. I hope, it will work 'from the box' soon.

SirSerje avatar Jul 18 '18 13:07 SirSerje

@SirSerje While it is probably a good idea to set a default Accept header, I just want to point out that the Accept header isn't the same as Content-Type. The Content-Type tells your server the format you, as the client, are sending the server. Accept tells your server the format in which you, as the client, want response data.

This bears repeating: if you're here and you want Content-Type to always be set because you're using it on the server side to figure out which format to send data back, then that's incorrect; you want to be using Accept instead. See this SO question.

mcmire avatar Jul 18 '18 15:07 mcmire