7

I'm sort of new to REST..

For full disclosure, I'm running this code inside of a Netlify Lambda function and testing via netlify-lambda.

My curl command works:

 curl -u "<username>:<password>" https://api.github.com/repos/<username>/<reponame>

But when I attempt a get request via axios I'm getting a 404 (which according to github docs implies an auth issue). This is what I'm doing (also doesn't work without the custom headers, I've just been trying random things).

axios({
    method: "get",
    url: `https://api.github.com/repos/${user}/<reponame>/`,
    headers: {
        Authorization: `Bearer ${githubToken}`,
        "Content-Type": "application/json"
    },
    auth: {
        username: user,
        password: pass
    }
    })
    .then(res => {
        callback(null, {
            statusCode: 200,
            body: JSON.stringify(res.data)
        });
    })
    .catch(err => {
        callback(err);
    });

One thing I noticed was that it seems axios was taking my username and password and prepending them to the url i.g. https://<username>:<password>@api.github.com/repos/<username>/<reponame>

Is this how auth should be sent over?

Esten
  • 1,385
  • 2
  • 12
  • 21
  • could you link the authentication you're trying or alteast the complete working curl – 1565986223 Apr 29 '19 at 02:55
  • Is this a copy/paste error in the Axios example?: `url: `https://api.github.com/repos/${user}//`,`. If not, you are substituting `${user}` but leaving `` as literal text (which would be a 404 error for sure). – Henry Mueller Apr 29 '19 at 05:12
  • No, I have a variable for my github user and pass and I'm simply omitting my personal repo name – Esten Apr 29 '19 at 13:45
  • @1556089774 I'm not sure what you are asking – Esten Apr 29 '19 at 18:19
  • I know you're saying that it fails with or without the custom headers but sending an Authorization header while using axios basic auth seems suspect. – Sam Alpher Apr 29 '19 at 19:43
  • @SamAlpher according to the docs, providing basic auth is supposed to overrule Authorization custom headers, but yeah, doesn't work with the header or the `auth` property – Esten Apr 29 '19 at 21:26
  • @Esten For what it's worth, I was able to run essentially the same code as what you posted with success. The caveat being that I'm not running the code as a lambda in netlify. – Sam Alpher Apr 30 '19 at 11:53
  • @Esten If it helps, here's the code I tested with sitting in a react boilerplate in the "Test" react component: https://github.com/samalpher/github-api-axios-test . I tested both bearer auth and basic auth and verified that both work. – Sam Alpher Apr 30 '19 at 12:24
  • @SamAlpher that is helpful, thank you. It does appear to be something with the environment perhaps. I was also able to run the request successfully in client code. – Esten Apr 30 '19 at 14:48
  • @SamAlpher ok, it is definitely an environment issue, apparently pertaining to my local debugging environment rather than nodejs. When I moved the code to production, I'm actually getting a response. I think it is to do with CORS: https://stackoverflow.com/questions/54096057/netlify-nodejs-function-always-returns-response-to-preflight-request-doesnt-pa Pretty amazing it was so difficult to distiguish between an auth issue and potentially some sort of local Chrome/CORS issue – Esten Apr 30 '19 at 15:08

2 Answers2

4

I shouldn't have had a trailing forward slash at the end of my URL.

Esten
  • 1,385
  • 2
  • 12
  • 21
3

If you already have a token you don’t need user/pass, just add the token to the header.