1

After following the documentation on GitHub's API, I got stuck on submitting a comment for a gist, the following code always returns 404, and the same call made in Postman too.

My JavaScript code as follows:

const config = {
        method: 'POST',
        headers: {
            'Authorization': credentials.authorizationHeader,
            'Content-Type': 'application/vnd.github.v3.text+json'
        },
        body: { "body": JSON.stringify(comment) } 
    };

    fetch(`https://api.github.com/gists/${gistId}/comments/`, config)
        .then(res => {
            if (res.ok) {
                dispatch(getGistDetails(gistId, credentials));

                dispatch({ type: SUBMIT_COMMENT_SUCCESS });
            } else {
                ToastAndroid.show('An error ocurred, please try again.', ToastAndroid.SHORT);
                console.log(res);
                dispatch({ type: SUBMIT_COMMENT_FAIL });
            }
        })
        .catch(err => console.log(err));

Credentials I'm getting via OAuth:

accessToken: "redacted"
authorizationHeader:"bearer redacted"
clientID:"redacted"
idToken:null
scopes:"gist"
type:"bearer"

I tried changing the authorizationHeader to token <oauth_token, but still no success.

Thanks in advance.

jacques mouette
  • 363
  • 3
  • 8

2 Answers2

0

Looks like you have some non standard characters in your GIST ID that are not even visible and I can't even get your link to work ( or is it private? )

Your query

Zilvinas
  • 5,518
  • 3
  • 26
  • 26
  • That's really strange, I copied and pasted directly from the address bar. Still, in my code, the gist `id` is coming from the API. I first request the gist details, which occurs without a problem, but then the comment fails. Let me try the address here again: `https://gist.github.com/desktp/8e376d6cd8d671e14c735d051eff7140` – jacques mouette Sep 06 '17 at 21:28
  • Nope, same thing again – Zilvinas Sep 06 '17 at 21:29
  • Check this out: https://stackoverflow.com/questions/18478847/why-is-8203-being-injected-into-my-html might just be your problem – Zilvinas Sep 06 '17 at 21:34
  • This pointed me to the direction I needed to fix the issue. I submitted an answer with the solution. – jacques mouette Sep 06 '17 at 21:45
0

Turns out I was overcomplicating, as getting a gist's details through the API also nets you a comments_url field with the correct url, so no need to splice strings, falling into the very strange issue mentioned by @Zilvinas below. Also, a minor adjustment in the body to

const body = { body: comment }

const config = {
    method: 'POST',
    headers: {
        'Authorization': credentials.authorizationHeader,
        'Content-Type': 'application/vnd.github.v3.text+json'
    },
    body: JSON.stringify(body)
};

fixed the subsequent Problems parsing JSON error I got.

jacques mouette
  • 363
  • 3
  • 8