Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Successful POST request's promise is rejected #29

Closed
alexpyoung opened this issue Dec 13, 2015 · 7 comments
Closed

Successful POST request's promise is rejected #29

alexpyoung opened this issue Dec 13, 2015 · 7 comments
Labels

Comments

@alexpyoung
Copy link

Hi, apologies for such a trivial problem but I'm really scratching my head here.

In my view model I configure the fetch client (example is attempting Github OAuth) like so:

constructor(
    http
) {

    http.configure(config => {

        config
            .useStandardConfiguration()
            .withBaseUrl('https://github.com/')
            .withDefaults({
                mode: 'no-cors',
                headers: {
                    'Accept': 'application/json'
                }
            });
    });

    this.http = http;
}

After successfully authorizing the client and receiving an verification code, I request an access token:

requestAccessToken() {

    let url = 'login/oauth/access_token';
    url += `?client_id=${ this.client_id }`;
    url += `&client_secret=${ this.client_secret }`;
    url += `&code=${ this.oauth_code }`;

    return this.http.fetch(url, {
        method: 'POST',
    });
}

The request returns a 200 status with the appropriate body, however the Promise returned is always rejected and I am unable to access the response. i.e.

requestAccessToken()
    .then(
        response => {
            // Never invoked
        },
        error => {
            // Always invoked
        }
    );
@bryanrsmith
Copy link
Contributor

What's the error that it rejects with? What do you see in the console? Without more information I can only guess, but I suspect it's because you're using mode: 'no-cors' on a cross-origin request. Do you get a different result if you change it back to the default of 'cors'?

@bryanrsmith
Copy link
Contributor

Feel free to reopen this if there's more I can do to help.

@alexpyoung
Copy link
Author

@bryanrsmith the error response looks like so:

screen shot 2015-12-14 at 8 28 36 pm

Is it possible to make CORS requests with the fetch client then if I exclude mode: 'no-cors'? Alternatively, I might try JSONP with the HTTP client.

@alexpyoung
Copy link
Author

Whoops. Trying to POST with JSONP is no fun. But it looks like you were looking in the right direction @bryanrsmith. Without mode: 'no-cors':

Fetch API cannot load https://github.com/login/oauth/access_token...
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:9000' is therefore not allowed access. 
If an opaque response serves your needs, 
set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Github indicates it supports CORS so I wonder what's going on. Is this an issue with the fetch-client?

@bryanrsmith
Copy link
Contributor

The type: "opaque" in the response means that you cannot read the response from javascript. This is why you're seeing the rejection, and it is expected from no-cors requests. You can read about this on MDN. As you can see from the second error message, GitHub is not attaching CORS headers to the response. I googled the URI you're requesting and found this: isaacs/github#330.

Hope this helps.

@alexpyoung
Copy link
Author

It looks like this might be a dead-end for my current implementation, as my client_secret is indeed exposed on my demo app. Appreciate the lead.

@KOueslati
Copy link

You can set a no-cors mode like the code below

var myHeaders = new Headers();
var myInit = { method: 'POST',
headers: myHeaders,
mode: 'no-cors',
cache: 'default' };

return this.http.fetch(url, myInit);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants