簡體   English   中英

如何修復“請求失敗,狀態碼為 BadRequest”RestSharp

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

我試圖發送一個帖子請求,但每次它給我“請求失敗,狀態碼 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);

如果你知道如何解決它,請告訴我

我希望這個例子對你有幫助。

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

    }

然后這樣稱呼它

                        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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM