代码之家  ›  专栏  ›  技术社区  ›  interesting-name-here

使用GitLab API进行RestSharp POST提交

  •  0
  • interesting-name-here  · 技术社区  · 7 年前

    GitLab API .

    我正在通过授权并获得 BadRequest 回答我已经验证了我的 JSON

    {“error”:“缺少分支,缺少commit_消息,缺少操作”}

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

    • 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"

    编辑 的堆栈跟踪

    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 回复  |  直到 7 年前
        1
  •  0
  •   interesting-name-here    7 年前

    结果是,我的 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);