RestSharp POST 与 GitLab 一起提交-API
RestSharp POST Commit with GitLab-API
我正在尝试将多文件提交发送到 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)
所以,我的 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);
令人惊讶的是,这么小的东西会让你疯狂几个小时。
我正在尝试将多文件提交发送到 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)
所以,我的 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);
令人惊讶的是,这么小的东西会让你疯狂几个小时。