user8589034
user8589034

Reputation:

get response status of 415 Unsupported Media Type REST client

I want to make a POST request to REST API running on Azure and I want to pass a javascript object with the POST request. But response shows 415 error code Unsupported Media type. I did try changing 'Content Type' to 'Application/json', but I get the same response.

componentDidMount() {

const  bodyFormData = new FormData();
bodyFormData.set('id', 30958);
axios({
  method: 'post',
  url: 'https://example./api/example/GetExamplData',
  data: bodyFormData,
  config: { headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    }}
  })
    .then((response) => {console.log(response)}) 
    .catch(error => {console.log( 'the error has occured: ' + error) })
}

Upvotes: 1

Views: 17981

Answers (1)

hendrathings
hendrathings

Reputation: 3765

Usually REST API use json mediaType, make sure your server.

Try this:

const  bodyFormData = { "name":"John", "age":30, "city":"New York"};
axios({
  method: 'post',
  url: 'https://example./api/example/GetExamplData',
  data: JSON.stringify(data),
  config: { headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json' 
    }}
  })
    .then((response) => {console.log(response)}) 
    .catch(error => {console.log( 'the error has occured: ' + error) })
}

make sure const bodyFormData = new FormData(); return json.

Upvotes: 3

Related Questions