2016-09-25 296 views
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)" 

你能幫我擺脫這個問題嗎?

非常感謝。

回答

0

你可能不得不在Content-Type -header添加到請求,太:

request.AddHeader("Content-Type", "application/json; charset=utf-8"); 

此外,您可以使用這樣一個Fiddler Web調試代理來看看實際的HTTP請求會被髮送到服務。

+0

謝謝你。 但添加內容類型後,我得到的結果相同 –

+0

您確定要發佈正確的內容嗎?你的'訂單'對象序列化是否正確? – khlr

相關問題