Search code examples
c#jsonrestsharpgitlab-api

RestSharp POST Commit with GitLab-API


I am trying to send a Multifile commit to the GitLab API.

I'm passing authorization and getting a BadRequest response. I've validated my JSON but I'm getting this in the response

"{"error":"branch is missing, commit_message is missing, actions is missing"}"

JSON

{
    "branch": "master",
    "commit_message": "Ticket-27 6\/29\/2017 4:37:13 PM",
    "author_name": "My Name",
    "author_email": "myName@myCompany.com",
    "actions": [{
            "action": "create",
            "file_path": "procedures\/dbo.sp_SomeProc1.sql",
            "content": "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n-- =============================================\r\n-- Author:\t\t<,,Name>\r\n-- Create date: <Create Date,,>\r\n-- Description:\t<Description,,>\r\n-- =============================================\r\n\r\nCREATE PROCEDURE [dbo].[sp_SomeProc1] @ID int,@StartDate datetime\r\n\t-- Add the parameters for the stored procedure here\r\n\r\nAS\r\nBEGIN\r\n\r\n\tSET NOCOUNT ON;\r\n\tSELECT * FROM MyTable WHERE MEMBERID = @ID \r\n\t"
        }, {
            "action": "create",
            "file_path": "procedures\/dbo.sp_SomeProc2.sql",
            "content": "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n-- =============================================\r\n-- Author:\t\t<,,Name>\r\n-- Create date: <Create Date,,>\r\n-- Description:\t<Description,,>\r\n-- =============================================\r\n\r\nCREATE PROCEDURE [dbo].[sp_SomeProc2] @ID int,@StartDate datetime\r\n\t-- Add the parameters for the stored procedure here\r\n\r\nAS\r\nBEGIN\r\n\r\n\tSET NOCOUNT ON;\r\n\tSELECT * FROM MyTable WHERE MEMBERID = @ID \r\n\t"
        }, {
            "action": "create",
            "file_path": "procedures\/dbo.sp_SomeProc3.sql",
            "content": "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n-- =============================================\r\n-- Author:\t\t<,,Name>\r\n-- Create date: <Create Date,,>\r\n-- Description:\t<Description,,>\r\n-- =============================================\r\n\r\nCREATE PROCEDURE [dbo].[sp_SomeProc3] @ID int,@StartDate datetime\r\n\t-- Add the parameters for the stored procedure here\r\n\r\nAS\r\nBEGIN\r\n\r\n\tSET NOCOUNT ON;\r\n\tSELECT * FROM MyTable WHERE MEMBERID = @ID \r\n\t"
        }
    ]
}

I then changed the content to be "test" but I get a different error I believe from RestSharp.

  • StatusCode = 0
  • ErrorMessage = "Object reference not set to an instance of an object."

JSON

{
    "branch": "master",
    "commit_message": "Ticket-27 6\/29\/2017 4:37:13 PM",
    "author_name": "My Name",
    "author_email": "myName@myCompany.com",
    "actions": [{
            "action": "create",
            "file_path": "procedures\/dbo.sp_SomeProc1.sql",
            "content": "test"
        }, {
            "action": "create",
            "file_path": "procedures\/dbo.sp_SomeProc2.sql",
            "content": "test"
        }
    ]
}

And finally, my C# code:

RestRequest request = 
    new RestRequest($@"api/v4/projects/{project.id}/repository/commits", Method.POST);

request.Parameters.Add(new Parameter() { 
    ContentType = "application/json", 
    Type = ParameterType.RequestBody, 
    Value = commit 
});

request.Parameters.Add(new Parameter() { 
    Name = "PRIVATE-TOKEN", 
    Type = ParameterType.HttpHeader, 
    Value = ConfigurationManager.AppSettings["GitLabPrivateToken"] 
});

request.RequestFormat = DataFormat.Json;

IRestResponse respone = _restClient.Execute(request);

Now, the "file_path" actually doesn't exist on the branch, but I thought since I was taking a "create" action it wouldn't need to be.

EDIT Stack trace of the StatusCode = 0

at RestSharp.RestClient.<ConfigureHttp>b__2f(Parameter p2)
at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
at RestSharp.RestClient.ConfigureHttp(IRestRequest request, IHttp http)
at RestSharp.RestClient.Execute(IRestRequest request, String httpMethod, Func`3 getResponse)

Solution

  • So, it turns out that the name of the parameter for my JSON request parameter needed to be application/json. After the change, worked fine. Here is the final C# code:

    RestRequest request = 
        new RestRequest($@"api/v4/projects/{project.id}/repository/commits", Method.POST);
    
    request.Parameters.Clear();
    
    request.Parameters.Add(new Parameter() { 
        Name = "application/json", 
        ContentType = "application/json", 
        Type = ParameterType.RequestBody, 
        Value = commit 
    });
    request.Parameters.Add(new Parameter() { 
        Name = "PRIVATE-TOKEN", 
        Type = ParameterType.HttpHeader, 
        Value = ConfigurationManager.AppSettings["GitLabPrivateKey"] 
    });
    
    IRestResponse respone = _restClient.Execute(request);
    

    Amazing how something so small will drive you bonkers for hours.