3

I have been trying to connect to GitHub's GraphQL from a JavaScript project running locally but the connection fails.

I using a personal_access_token created on GitHub, the token is correct.

Strangely I have tried testing the token using a curl command (supplied by the good guys at GitHub):

$ curl -v -H "Authorization: bearer TOKEN" -X POST -d "{ \"query\": \"query { viewer { login }}\" }" 'https://api.github.com/graphql'

but the curl command fails to do anything from my machine - it DOES however work fine from other machines. On my machine the command just sits on the next line's cursor never failing or completing:

 > 

This has been looked at by GitHub support and I've tried: * different ways to connect (JS, GraphQL, curl) * creating new token * creating token with different permissions

But all have the exact same result - which suggests there is something different/wrong with the setup on my machine/network but I can't find anything which would affect this.

Any suggestions?

Rich
  • 970
  • 2
  • 16
  • 42
  • 1
    Being dropped to the `>` suggests that you may have mismatched quotes. You could simplify the argument to `-d` by surrounding it with single quotes, which makes the escaping of its double quotes unnecessary: `curl -v -H 'Authorization: bearer TOKEN' -X POST -d '{ "query": "query { viewer { login }}" }' https://api.github.com/graphql` – cody Sep 29 '19 at 13:53
  • do you have curl installed on your mac? `brew install curl` – andrewgi Sep 29 '19 at 16:19
  • @andrewgi yes I do – Rich Sep 30 '19 at 12:19

2 Answers2

1

Another approach would be to use a JSON file instead than a JSON expression that you have to surround appropriately with single or double quotes.

Plus, you can validate your JSON file in advance form command-line.
And you don't have to worry about double-quotes used in said file.

curl -v -H "Authorization: bearer TOKEN" -X POST -d @afile.json https://api.github.com/graphql

If not, the official example from GitHub was:

curl -H "Authorization: bearer token" -X POST -d " \
 { \
   \"query\": \"query { viewer { login }}\" \
 } \
" https://api.github.com/graphql

In both cases, no single quotes were needed around the URL https://api.github.com/graphql.
So try your original command without them first.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks but I get the same issue, the token is still failing using this method as well – Rich Oct 02 '19 at 18:40
  • @Rich Do you have any alias or any "curl" wrapper which might take over the system curl and triggers a different behavior? What does `alias|grep -i curl` return? – VonC Oct 02 '19 at 18:43
  • What is your [current and default shell](https://stackoverflow.com/a/43417758/6309), as well as your [`curl` version](https://stackoverflow.com/a/13449098/6309)? – VonC Oct 02 '19 at 18:54
0

Very hard to know what your issue is with the information provided. Your command works for me on my Mac.

What terminal are you using? Are you copying and pasting text? Is it possible the copied text somehow has "smart quotes", using and instead of "? What shell (and what version of the shell) are you using? What does command -V curl output? Which version of curl are you using?

I suggest you try this:

/usr/bin/curl -v -H "Authorization: bearer $GITHUB_TOKEN" -X POST --data-binary '{ "query": "query { viewer { login } }" }' 'https://api.github.com/graphql'
Old Pro
  • 24,624
  • 7
  • 58
  • 106