javascript - get response status of 415 Unsupported Media Type REST client - Stack Overflow

I want to make a POST request to REST API running on Azure and I want to pass a javascript object with

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.

ponentDidMount() {

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) })
}

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.

ponentDidMount() {

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) })
}
Share Improve this question asked Mar 12, 2018 at 15:55 user8589034user8589034 1
  • not sure but here's a quick shot: set your headers as a standalone property (don't wrap it inside config) – Kox Commented Mar 12, 2018 at 16:37
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744170289a4561503.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信