0

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

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

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

然后我将内容更改为,"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)
4

1 回答 1

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

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

于 2017-06-30T13:43:38.507 回答