Search code examples
pythonhttp-postpython-requestsgitlabgitlab-api

How to create a MR using GitLab API?


I am trying to create a merge request using the GitLab Merge Request API with python and the python requests package. This is a snippet of my code

import requests, json

MR = 'http://www.gitlab.com/api/v4/projects/317/merge_requests'

id = '317'
gitlabAccessToken = 'MySecretAccessToken'
sourceBranch = 'issue110'
targetBranch = 'master'
title = 'title'
description = 'description'

header = { 'PRIVATE-TOKEN' : gitlabAccessToken,
           'id'            : id,    
           'title'         : title, 
           'source_branch' : sourceBranch, 
           'target_branch' : targetBranch
         }

reply = requests.post(MR, headers = header)
status = json.loads(reply.text)

but I keep getting the following message in the reply

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

What should I change in my request to make it work?


Solution

  • Apart from PRIVATE-TOKEN, all the parameters should be passed as form-encoded parameters, meaning:

    header = {'PRIVATE-TOKEN' : gitlabAccessToken}
    params = {
               'id'            : id,    
               'title'         : title, 
               'source_branch' : sourceBranch, 
               'target_branch' : targetBranch
             }
    
    reply = requests.post(MR, data=params, headers=header)