Opening merge request in GitLab by curl

3
Where we work, we use a variation of GitFlow where when a hotfix enters the product code, there is only the merge for master which, when approved, propagates to develop through the master 2 develop branch % (or simply m2d ).

I am making a curl request to open merge request . I currently have the following (already with the creation of the branch m2d as much updated as possible):

git fetch
git push origin +origin/master:refs/heads/m2d

curl --request POST https://gitlab.com/api/v4/projects/${project_id}/merge_requests --header "PRIVATE-TOKEN: ${mytoken}" --data "{
                \"id\": \"${project_id}\",
                \"title\": \"m2d\",
                \"source_branch\": \"m2d\",
                \"target_branch\": \"develop\"
        }"

However, I get the following output from curl :

{"error":"title is missing, source_branch is missing, target_branch is missing"}

I tried to follow the tips in the International Stack Overflow, putting the relevant information / JSON in the body of the submission, but I could not figure out what I'm doing wrong.

I can, with the same token, successfully make the following request:

curl https://gitlab.com/api/v4/projects/${project_id} --header "PRIVATE-TOKEN: ${mytoken}"
    
asked by anonymous 04.07.2018 / 05:10

1 answer

1

As well indicated by the user @NoobSaibot in comment in the question, there is a GitLab blog post just right about this subject. So specific. Look at the title in free translation:

  

How to automatically create a new merge request in GitLab with GitLab-CI

There, it gives several examples of using the curl command.

After reading this publication, I noticed that there are 2 ways to fix the command.

Solution using JSON

Just inform the MIME-type of sending my content. Just set the content-type in header :

git fetch
git push origin +origin/master:refs/heads/m2d

curl --request POST https://gitlab.com/api/v4/projects/${project_id}/merge_requests --header "PRIVATE-TOKEN: ${mytoken}" \
  --header 'Content-Type: application/json' \
  --data "{
            \"id\": \"${project_id}\",
            \"title\": \"m2d\",
            \"source_branch\": \"m2d\",
            \"target_branch\": \"develop\"
    }"

I just added the following header to the request: Content-Type: application/json

Solution using form

Basically, using the curl directive to send forms, the CLI flag --form .

curl --request POST https://gitlab.com/api/v4/projects/${project_id}/merge_requests \
    --header "PRIVATE-TOKEN: ${mytoken}" \
    --form "id=${project_id}" \
    --form 'title=m2d' \
    --form 'source_branch=m2d' \
    --form 'target_branch=develop'
    
05.07.2018 / 04:58