2015-12-26 233 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 –