2015-12-26 232 views
1

當我使用RestSharp PCL POST POST某些JSON時,我得到HTTP 400。 當我發送一個字符串時,似乎包含\"。它不應該。這可能是POST不起作用的原因。使用POST時HTTP 400錯誤請求

我可能錯過了一些我需要填寫的內容,但請幫助我理解我失蹤的內容。

這裏是代碼我使用

public async Task<bool> DoPost<T>(string endPoint, T content) where T : class 
{ 
    var body = JsonConvert.SerializeObject(content); 
    var request = new RestRequest(endPoint, Method.POST); 
    request.AddParameter("application/json", body, ParameterType.RequestBody); 
    try 
    { 
     var response = await _client.Execute(request, _cancellationToken.Token); 
     if (response.IsSuccess) 
     { 
      return true; 
     } 
    } 
    catch (Exception e) 
    { 
     throw new GTSWebServiceException(e.Message, e); 
    } 

    return false; 
} 

回答

1

你檢查這一點:How to POST request using RestSharp我知道你是包括第一個參數的內容類型,但也許你可以RequestFormat玩?我懷疑這是必要的。另外,你是否檢查過你的字符串是否實際上包含一個像雙引號一樣的轉義字符?如果你也看到斜線字符串可能也是因爲你正在調試它?你在服務器中通過什麼方式收到的有效負載會返回錯誤的請求?

+0

雖然Restharp portable沒有requestformat。服務器包含雙引號。當我收回字符串時,它給了我一個帶有引號的字符串。 –

+0

你試過這個然後:http://stackoverflow.com/questions/13833900/return-json-but-it-includes-backward-slashes-which-i-dont-want我假設你有一個WebAPI後端 –

+0

沒有適用於RestSharp Portable –