Invoke-WebRequest failed - "Problems parsing json" with GitHub api
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"}
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 $headersWrite-Host $response
Output: {"data":{"viewer":{"login":"yourGithubUsername"}}}