Github Rest API using fetch with AWS Lambda returning 403 Forbidden

Hi all,

I have some nodeJS code that works locally and I’ve also validated that correct url and parms with postman, but when I run this code on AWS Lambda, I get a 403 Forbidden. I am successfully able to create a new file in my repo, but only run into issues when trying to run in AWS Lambda. I also tried running it in a Cloudflare Worker just to see if it was an issue with Lambdas.

I’ve tried running down the route of it being a CORS issue, but no success there. Any pointers would be greatly appreciated at this point.

Here is the code:

const _response = await fetch('https://api.github.com/repos/<redacted-owner>/<redacted-repo>/contents/<redacted-path>, {
    method: 'PUT',
    headers: {
        "Content-Type" : "application/vnd.github.v3+json",
        "Authorization" : "token <redacted-token>"
    },
    body: JSON.stringify(_bodyObj)
})

It’s tough to guess at the problem here without being able to debug.

Have you checked CloudWatch logs? All Lambda functions should be logging there.

You also may need a User-Agent field in your Authorization header.

Thanks for replying! Unfortunately, I don’t see anything in the logs. I’m using try/catch blocks and no errors are being trapped. And it may not be clear from my code above, but I am also encoding the content of my commit to Base64.

Here are my params that I’m passing:

{
message: ‘Update test.html’,
branch: ‘stage’,
content: Base64.encode(_content),
}

@dan-lampman , thank you! You were right! I was missing User-Agent field.

1 Like