0
我在调用RestAPI并以json格式发送数据。使用RestSharp作为客户端。StatusCode:BadRequest - 使用RestSharp调用RestAPI
有以下功能。请看看它。
public HttpStatusCode CreatOrder(OrderRequest order,string token)
{
var client = new RestClient("this is url of api");
request.AddHeader("Authorization", token);
var json = JsonConvert.SerializeObject(order); //using Json.Net
request.AddJsonBody(order);
request.Method = Method.POST;
request.AddHeader("Content-Type", "application/json; charset=utf-8");
var response = client.Execute(request);
return response.StatusCode;
}
我可以在调试过程中看到下面的json。
"{\"waypoints\":[{\"exactLatLng\":[{\"lat\":123.0,\"lng\":234.0}],\"premise\":null,\"houseNumber\":null,\"street\":null,\"subLocality\":null,\"locality\":null,\"city\":null,\"district\":null,\"province\":null,\"country\":null,\"postalCode\":null,\"countryCode\":null,\"poiName\":null,\"placeLatLng\":[{\"lat\":123.0,\"lng\":234.0}]}],\"extraOptions\":null,\"client\":{\"clientId\":null,\"name\":\"NumanTest\",\"phone\":\"1213123\",\"imageUrl\":null},\"name\":\"NumanTest\",\"notes\":null,\"unitOfLength\":[],\"tripDistance\":null,\"tripDuration\":null,\"pickupTime\":null,\"numberOfSeats\":null,\"vehicleType\":[],\"tariffType\":[],\"paymentMethods\":[],\"prepaid\":null,\"tariffId\":null}"
但我总是收到不良要求的回复。
"StatusCode: BadRequest, Content-Type: application/json; charset=utf-8, Content-Length: 2325)"
你能帮我摆脱这个问题吗?
非常感谢。
谢谢你。 但添加内容类型后,我得到的结果相同 –
您确定要发布正确的内容吗?你的'订单'对象序列化是否正确? – khlr