简体   繁体   English

如何修复“请求失败,状态码为 BadRequest”RestSharp

[英]How do i fix a "Request failed with status code BadRequest" RestSharp

Im trying to send a post request but everytime it gives me " Request failed with status code BadRequest" how do i fix this error i have tried stuff like adding headers but i still got the same error我试图发送一个帖子请求,但每次它给我“请求失败,状态码 BadRequest”我该如何解决这个错误我已经尝试过添加标题之类的东西,但我仍然遇到同样的错误

 var client = new RestClient(options);
       var request = new RestRequest()
           .AddQueryParameter("address", "jioaksiokar@cutradition.com")
           .AddQueryParameter("password", "aasfasdasdsf");
       var response = await client.PostAsync<MyResponse>(request);
       Console.WriteLine(response);

if you know how to fix it please tell me ty如果你知道如何解决它,请告诉我

I Hope this example helps you.我希望这个例子对你有帮助。

        public async Task<JsonResult> CallApi(
        string serverAddress,
        string apiAddress,
        string body
        )
    {

        var client = new HttpClient
        {
            BaseAddress = new Uri(serverAddress)
        };

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json-patch+json"));

        client.DefaultRequestHeaders.Add($"Authorization", $"Basic [write your encoded password here]");

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, apiAddress);
        request.Content = new StringContent(body,
                                            Encoding.UTF8,
                                            "application/json-patch+json");

        HttpResponseMessage response = client.SendAsync(request).Result;

        string jsonString = await response.Content.ReadAsStringAsync();

        return JsonConvert.DeserializeObject<JsonResult>(jsonString);

    }

and then call it like this然后这样称呼它

                        dynamic array =
                        new[]
                        {
                            new {
                                op = "add",
                                path = "/fields/System.Title",
                                from = (string) null,
                                value = "test1"
                            },
                            new {
                                op = "add",
                                path = "/fields/System.Description",
                                from = (string) null,
                                value = "test2"
                            },
                            new {
                                op = "add",
                                path = "/fields/System.History",
                                from = (string) null,
                                value = "test3"
                            },
                            new {
                                op = "add",
                                path = "/fields/System.AssignedTo",
                                from = (string) null,
                                value = "test4"
                            }
                        };

                    JsonResult jsonResult = new ApiWorks().CallAzureApi(
                        @"http://127.0.0.1/",
                        @"project1/_apis/wit/workitems/$User Story?api-version=6.0",
                        JsonConvert.SerializeObject(array)
                        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestSharp + Xamarin - “请求失败,状态码 InternalServerError” - RestSharp + Xamarin - "Request failed with status code InternalServerError" 如何在RestSharp中发送POST请求? - How do I send a POST request in RestSharp? 如何通过将 HttpClient 请求转换为 RestSharp 请求来上传文档? - How do I upload document by converting HttpClient request to RestSharp request? 如何在 Restsharp 中清除请求的所有参数? - How do I clear all parameters of a request in Restsharp? 如何在发布请求正文 (RestSharp) 中格式化和发布 Json - How do I format and post Json in the post request body (RestSharp) 如何从发布请求中获得响应? 锐化 - How do i get the response from a post request? Restsharp RestSharp - 如何获得数字 http 响应代码? - RestSharp - How do I get the numerical http response code? RestSharp和Twitter API 1.1:如何正确URLEncode状态更新文本? - RestSharp and Twitter API 1.1: How do I correctly URLEncode status update text? WCF错误:“处理Web请求时出错:状态码400(BadRequest)”是否是消息大小错误? - WCF Error: 'There was an error on processing web request: Status code 400(BadRequest)' Is it Message Size Error? 给定以下方法,我如何使用 RestSharp 将请求发送到 Bitstamp API? - How do I, given the following methods, send the request to the Bitstamp API using RestSharp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM