简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM