[英]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.