2

I'm doing an API CALL width POST method either with Axios or Fetch but send the wrong Content-Type on Android.

Send application/json; charset=utf-8 instead of application/json

It's working fine on iOS and GET method seems to work well on both platform.

1) My App was created with Create-React-Native App and working with Expo

2) What I've tried so far :

  • I read a lot of topics (github / stackoverflow) with some changes to do : in the headers, send another content-type, changes received data in Postman, etc- Using Axios or Fetch

Example: 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'

Code I tried (basically the same)

fetch('URL', {
  method: 'POST',
  headers: {
    'TOKEN': 'TOKEN',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
}).then((response) => console.log(response))

or

axios({
  method: 'post',
  url: LOGIN_API_URL,
  headers: {
    'Content-type': 'application/json',
    'TOKEN': 'MyTestToken',
  },
  data: data,
}).then((response) => {console.log(response.data)})

The error I get :

{title: "ERROR", code: "invalid ContentType", description: "Content-Type expeced: application/json, Content-Type received: application/json; charset=utf-8", success: false}

I would like to send the right content-type: 'application/json' for both platform : ios and android

If I missed some topics with the answer, sorry :)

Clément CREUSAT
  • 301
  • 3
  • 6
  • 13

1 Answers1

2

this looks like a bug on axios side (see https://github.com/axios/axios/issues/859 ) There seem to be a possible solution linked to whether the data parameter is provided or not (https://github.com/axios/axios/issues/86):

https://github.com/axios/axios/issues/86#issuecomment-405930811

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

I hope this can point you in the right direction.

Lucat
  • 2,242
  • 1
  • 30
  • 41