Search code examples
restcurlpostgitlabgitlab-api

Curl POST gitlab API gives 404


I am trying to push a file to my folder called "collections" in my repository using curl. I've spent almost 2 days investigating problem and I am not sure what is the exact problem.

curl -D- -k -X GET -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections

Get request works properly and I get list of files in collections folder. The collections folder is a folder in my gitlab repository But when I try to POST a file to that exact same folder I get 404:

curl -D- -k -X POST -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" -F "file=@C:/Documents/Folder_A/bp30_QA.csv" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections

Am I missing some parameter? also gitlab API didn't help me very much.

Edit: Solution from Bertrand Martel helped me solve the issue

Also for everyone on windows having trouble installing jq

jq is a lightweight and flexible command-line JSON processor.

Install choco: https://chocolatey.org/install

Open powershell as administrator and run:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

After installation, run:

choco install jq

Solution

  • To create a new file called bp30_QA.csv in the collections folder, you can use the following :

    curl -H 'PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN' \
         -H "Content-Type: application/json" \
         -d '{
            "branch": "master", 
            "author_email": "[email protected]", 
            "author_name": "John Doe",
            "content": '"$(jq -Rs '.' bp30_QA.csv)"', 
            "commit_message": "create a new file"
        }' "https://gitlab.com/api/v4/projects/592/repository/files/collections%2Fbp30_QA.csv"
    

    It uses to wrap the content of the file in a single JSON field (check this post)