0

I tried the following code for creating a repository. But it gives JSON parsing problem. What causes this?. I took this code from https://docs.github.com/en/rest/reference/repos tried in python code using requests.post method. But that too gave the same error Problem Parsing JSON. Is this bad request or the parsing problem is inside Github API.

curl -i -H "Authorization: token MYACCESSTOKENHERE" \
    -d '{ \
        "name": "simp", \
        "auto_init": true, \
        "private": true, \
        "gitignore_template": "nanoc" \
      }' \
    https://api.github.com/user/repos

Output:

HTTP/1.1 400 Bad Request
Date: Sun, 09 Aug 2020 05:14:23 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 159
Server: GitHub.com
Status: 400 Bad Request
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4956
X-RateLimit-Reset: 1596950918
X-OAuth-Scopes: admin:gpg_key, admin:org, admin:org_hook, admin:repo_hook, gist, notifications, read:packages, repo, user, workflow, write:discussion, write:packages
X-Accepted-OAuth-Scopes: public_repo, repo
X-GitHub-Media-Type: github.v3; format=json
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-GitHub-Request-Id: B649:25C9:CDEF3A:117E0EE:5F2F862F

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"
}

R Nanthak
  • 344
  • 3
  • 17

2 Answers2

0

Could you please validate your token is authorized. Please refer Authorizing OAuth Apps

curl -H "Authorization: token OAUTH-TOKEN" <https://<your_repo_endpoint> -I

Update using JSON formatter

"-d""{ \\
        \"name\": \"simp\", \\
        \"auto_init\": true, \\
        \"private\": true, \\
        \"gitignore_template\": \"nanoc\" \\
      }""\\
  • The token I use is not the problem, otherwise, it would have pointed out that authorization error. But the problem is in the body of JSON. – R Nanthak Aug 09 '20 at 06:01
  • @RNanthak have tried using a JSON parser to see the formatting and found it like below "-d""{ \\ \"name\": \"simp\", \\ \"auto_init\": true, \\ \"private\": true, \\ \"gitignore_template\": \"nanoc\" \\ }""\\ – albert sebastian Aug 09 '20 at 07:15
  • Thanks @albertsebastian. I found it. Now I used ```curl -i -H "Authorization: token 1f003ae8eab2feea72630d6f3150b921a522a868" -d '{ "name": "simp1", "private": true}' https://api.github.com/user/repos```. After removing the unnecessary lines it worked. – R Nanthak Aug 09 '20 at 08:06
  • Hi @RNanthak Happy to hear back from you that you have figured out the right format. – albert sebastian Aug 09 '20 at 13:21
0

The problem is with the JSON format. After I removed unwanted lines and spaces from the curl command, it worked.

curl -i -H "Authorization: token 1f003ae8eab2feea72630d6f3150b921a522a868" -d '{ "name": "simp1", "private": true}' https://api.github.com/user/repos
R Nanthak
  • 344
  • 3
  • 17