0

I'am sending content type as Text/csv as my API accept only the text/csv type but the file's content type is showing as application/vnd.ms-excel.

I've also checked with the postman, API is accepting the file in the http request but when I'am sending through my browser application it returns the response unsupported media type

How can I solve this.

Please go through the code:

 var Authorization=window.sessionStorage.getItem('acesstToken')
   var Headers={
      'Authorization': 'Bearer '+Authorization,
      'Content-Type': 'text/csv',
     }
  const fd = new FormData()
  fd.append('csv_file',fieldValues.file[0],fieldValues.file[0].name);
  var url="XXXXXXXXXXXXXXX";
  fetch(url,{
    method : 'POST',
    headers: Headers,
    body: fd,
  })

1 Answers1

0

Here's my bet.

  1. Possible syntax issue. You're missing the closing quote for the 'Content-Type': 'text/csv'. Are you sure there aren't any syntax errors in your console?

  2. If it works with Postman (it accepts the file), but it doesn't work through your browser application, my bet is that could be an issue is with the same-origin policy.

    In the no-cors mode, the browser is limited to sending “simple” requests — those with safelisted methods and safelisted headers only. To send a cross-origin request with headers like Authorization, you have to drop the no-cors mode and support preflight requests (OPTIONS) for your API. See this answer.

Community
  • 1
  • 1
Kaloyan Kosev
  • 12,483
  • 8
  • 59
  • 90