Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch POST is returning HTTP 415, while curl goes on fine and returns result

This is what my code looks like

let body = {
            authCode: "XXXX",
            clientId: "YYYYYY",
            clientSecret: "ZZZZZZ"
        };


        fetch('https://api.myapp.com/oauth/token',{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            mode: 'no-cors',
            body: body
        }).then(function(response){
            console.log("response: ", response);
        }).catch(function(error){
            console.log("could not get tokens: ", error);
        })

In Chrome, this is what I see enter image description here

I tried to do this by curl command and this is what it looks like

➜  ~ curl -X POST -H "Content-Type: application/json" -d '{
  "authCode": "XXXX",
  "clientId": "YYYYY",
  "clientSecret": "ZZZZZZZZ"
}' https://api.myapp.com/oauth/token
{"authToken":"e141kjhkwr3432ca9b3d2128385ad91db4cd2:cca6b151-cab4-4de2-81db-9a739a62ae88:23000000"}

What am I doing wrong here?

UPDATE

After changing it to following, the result is still HTTP 415

fetch('https://api.myapp.com/oauth/token',{
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            mode: 'no-cors',
            body: JSON.stringify(body)
        }).then(function(response){
            console.log("response: ", response);
        }).catch(function(error){
            console.log("could not get tokens: ", error);
        })

Interestingly, I realized that I sent the header "Content-Type": "application/json" while what I get back is content-type: text/plain, why that might be happening?

enter image description here

like image 317
daydreamer Avatar asked May 26 '17 02:05

daydreamer


People also ask

Does the post method work with Curl command?

The POST method works absolutely fine on curl command. Please help. If it works with curl, what is different about the call you are making with whatever is failing? You must be a registered user to add a comment.

What does the HTTP 415 Unsupported Media type error response code mean?

Related topics. The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

Why is my API request not responding to Cors headers?

The error message appears to be clear, the request is cross origin and the server does not respond with CORS headers. You need to combat CORS first of all. You can't make API requests against a different domain:port than the one from which it was served by development server.

Is it possible to run curl from the Linux command line?

Only the client is different, i fire the Curl command from Linux command line which works fine, but when try to hit the API using the AJAX jquery call (using the same authenticatio), i get 403 forbidden error You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.


2 Answers

fetch() does not expect a JavaScript object at body. curl command and fetch() pattern are not the same. Use body: JSON.stringify(<javascript plain object>).

Request

Note: The body type can only be a Blob, BufferSource, FormData, URLSearchParams, USVString or ReadableStream type, so for adding a JSON object to the payload you need to stringify that object.

See Fetch with ReadableStream for status of implementation of ReadableStream set as value for body.

like image 97
guest271314 Avatar answered Oct 05 '22 22:10

guest271314


you must define Accept and Content-Type headers like that and stringify your data object

const params = {
            headers: {
                'Accept': "application/json, text/plain, */*",
                'Content-Type': "application/json;charset=utf-8"
            },
            body: JSON.stringify(yourObject),
            method: "POST"
        };

fetch(url, params)
        .then(data => { console.log('data', data)})
        .then(res => { console.log('res', res) })
        .catch(error => { console.log('error', error) });
like image 21
Samir Ghoneim Avatar answered Oct 06 '22 00:10

Samir Ghoneim