How to change Content-Type to application/json React

Viewed 15119

I'm using axios to get contents from an api. I want to set Content-Type to application/json in React using axios. What needs to be corrected? Below is the code for reference.

const config = {
  headers: {
    'Content-Type': 'application/json',
    'accept':'application/json'
  },
};

export function getDetails(){
    return function (dispatch) {
    return axios.get('url',config)
      .then(response => {
        if (response.status === 200) {
          console.log("actions ",response.data);
        }
      }).catch(err => {

      });
  }
}
1 Answers

When you do not send data (IE: no data in POST request or simple GET) axios removes content-type from headers. The solution for now seem to be to add an empty data object to the request.

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

Ref: https://github.com/axios/axios/issues/86

Related