1

I am trying to create a release using Github's API.

My request works fine in Postman but no matter what I've tried it always fails with curl, including if I just translate my Postman request into curl using Postman.

This is the body of my Postman POST request:

{
    "tag_name": "4.2.0",
    "target_commitish": "master",
    "name": "4.2.0",
    "body": "test"
}

I've included an authorization header of type "Basic", where I input my username and a token I have created for this purpose.
I am executing the request against https://api.github.com/repos/<myUsername>/<myRepo>/releases.

As I said - it works fine, but when I translate it into curl I get the error "Problems parsing JSON".

The translated curl command is:

curl --location --request POST 'https://api.github.com/repos/<myUsername>/<myRepo>/releases' \
--header 'Authorization: Basic <someHashOrSomething>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "tag_name": "4.2.0",
    "target_commitish": "master",
    "name": "4.2.0",
    "body": "test"
}'

which I reformat into curl --location --request POST 'https://api.github.com/repos/<myUsername>/<myRepo>/releases' --header 'Authorization: Basic <someHashOrSomething>' --header 'Content-Type: application/json' --data-raw '{ "tag_name": "4.2.0", "target_commitish": "master", "name": "4.2.0", "body": "test"}' to be on one line.

I also tried (since only the "tag_name" parameter is required):
curl -i -H 'Authorization: token <myToken>' -d '{"tag_name":"4.2.0"}' https://api.github.com/repos/<myUsername>/<myRepo>/releases
curl -i -H 'Authorization: token <myToken>' -d '{"tag_name":"4.2.0"}' https://api.github.com/repos/<myUsername>/<myRepo>/releases --header Content-Type:application/json
curl -d '{"tag_name":"4.2.0"}' -u <myUsername>:<myToken> https://api.github.com/repos/<myUsername>/<myRepo>/releases --header "Content-Type:application/json"
curl -d "tag_name=4.2.0" -u <myUsername>:<myToken> https://api.github.com/repos/<myUsername>/<myRepo>/releases --header "Content-Type:application/json"

Every curl request fails with the "Problems parsing JSON" error.

Edric
  • 24,639
  • 13
  • 81
  • 91
J. Doe
  • 99
  • 1
  • 6
  • Im curious why you use `--data-raw` here rather than just `--data`, as seen here https://stackoverflow.com/a/7173011/1363815 – Brendan Forster Apr 11 '20 at 16:48
  • There may also be some issues with correctly escaping the quotes in the JSON, depending on which OS and shell you are using (see comments) – Brendan Forster Apr 11 '20 at 17:29

3 Answers3

4

If you are doing this from the windows cmd prompt, you need to change

--data-raw '{   "tag_name": "4.2.0",    "target_commitish": "master",   "name": "4.2.0",    "body": "test"}'

to

--data-raw "{   \"tag_name\": \"4.2.0\",    \"target_commitish\": \"master\",   \"name\": \"4.2.0\",    \"body\": \"test\"}"

cmd prompt appears to not like single quotes for the data.

Jon
  • 41
  • 2
0

How to debug:

  • Add "-v --trace-ascii -" to the curl command to see log for command.
  • Use echo or write-host for your data in cmd or PowerShell

Remember that It is so important to make sure that curl receives valid JSON data.

See an example of Powershell here:

Bashir Momen
  • 1,461
  • 19
  • 17
-3

The problem could be your API key.

Check if it has the sufficient permissions or try and create a new one.

user13288253
  • 513
  • 1
  • 4
  • 9