5

I am attempting to communicate with the Graphql api through powershell. According to Github, one must first do the following curl call.

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

Using GitHub Enterprise, on powershell I do the following calls:

$url = "http://github.company.com/api/graphql" # note that it's http, not https

$body = "`"query`":`"query { viewer { login }}`""                                               #`

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add("content-type","application/json")

$headers.Add("Authorization","bearer myTokenNumber")

$response = Invoke-WebRequest -Uri $url -Method POST -Body $body -Headers $headers

I keep getting the same error message, that there are problems parsing JSON.

I assume the error is with the body tag, but I can't see how.

echo $body gives "query":"query { viewer { login }}"

What is the issue here?

Exact error message:

Invoke-WebRequest : {"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3"}
At line:1 char:13
+ $response = Invoke-WebRequest -Uri $url -Method POST -Body $body -Headers $heade ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
7
  • Can you please share the exact error message? Commented Feb 8, 2018 at 23:07
  • Please check now Commented Feb 8, 2018 at 23:10
  • Have you tested your token and Query in GitHub's GraphQL Explorer Interface ? Commented Feb 8, 2018 at 23:13
  • Works perfectly there Commented Feb 8, 2018 at 23:13
  • Is there a reason why you have to use the http version? Commented Feb 8, 2018 at 23:14

2 Answers 2

4

Your $body value is malformed JSON, because it is missing the enclosing { ... }.

Using a here-string makes the construction of the JSON string easier:

$body = @'
{ "query": "query { viewer { login } }" }
'@

Similarly, you can simplify building the headers with a hashtable literal:

$headers = @{
  "content-type" = "application/json"
  "Authorization" = "bearer myTokenNumber"
}
Sign up to request clarification or add additional context in comments.

Comments

4

Here is the working program. Thanks to those who answered:

$url = "https://api.github.com/graphql" # regular github
# for enterprise it will be http(s)://[hostname]/api/graphql where hostname is 
# usually github.company.com ... try with both http and https

$body = @'
{ "query": "query { viewer { login } }" }
'@

$headers = @{
  "content-type" = "application/json"
  "Authorization" = "bearer tokenCode"
}

$response = Invoke-WebRequest -Uri $url -Method POST -Body $body -Headers $headers
Write-Host $response

Output: {"data":{"viewer":{"login":"yourGithubUsername"}}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.