使用GitLab-API进行RestSharp POST提交

时间:2017-06-29 21:11:45

标签: c# json restsharp gitlab-api

我正在尝试向GitLab API发送多文件提交。

我正在通过授权并获得BadRequest响应。我已经验证了我的JSON,但我在回复中得到了这个

  

“{”error“:”分支丢失,缺少commit_message,缺少操作“}”

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"
        }
    ]
}

然后我将内容更改为"test",但我从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"
        }
    ]
}

最后,我的C#代码:

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);

现在,"file_path"实际上并不存在于分支上,但我认为自从我采取"create"动作后就不需要了。

修改 堆栈跟踪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)

1 个答案:

答案 0 :(得分:0)

因此,我发现JSON请求参数的参数名称必须为application/json。改变后,工作正常。这是最终的C#代码:

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);

令人惊讶的是,这么小的东西会让你疯狂几个小时。