3

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.

2
  • Im curious why you use --data-raw here rather than just --data, as seen here stackoverflow.com/a/7173011/1363815 Commented Apr 11, 2020 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) Commented Apr 11, 2020 at 17:29

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Confirmed, use regular quotation for the json string and escape quotation within. Bummer because this prevents easy copy&paste.
1

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:

Comments

-5

The problem could be your API key.

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

2 Comments

This is not an answer. Use the comment section for such message.
@SwatantraKumar it is so an answer, I had the same problem some time ago and that's how I fixed it.

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.