Search code examples
windowsbatch-filegithubcurlrepository

Curl request to batch or windows command line


I would like to create a github repository using a command line/batch script or whatever on windows. This request works on https://reqbin.com/curl, but I can't get it to work on windows. Thanks

curl -i -H "Authorization: token MY_TOKEN"
-d '{
    "name": "test",
    "auto_init": true,
    "private": false
  }' 
https://api.github.com/user/repos

Solution

  • curl on Windows is weird and doesn't recognize ' as a valid character, so you need double quotes for everything. Unfortunately, the only way to have double quotes inside of double quotes is to escape the inner quotes:

    curl -ki -X POST -H "Accept: application/vnd.github.v3+json" -u "username:TOKEN" -d "{\"name\": \"test\", \"auto_init\": true, \"private\": false}" https://api.github.com/user/repos
    

    Replace username with your actual username and TOKEN with your auth token. You also need -X POST in order to create repos, and the header string that I've added is optional, but strongly recommended.

    (Note that while the escape character in batch is ^, the escape character that curl looks for is \. Like I said, curl on Windows is weird.) I've also put everything on one line because multi-line commands have varying degrees of success, while a one-liner always works (as long as it's valid, of course).

    See https://docs.github.com/en/rest/repos/repos#create-a-repository-for-the-authenticated-user for more options.