1

I am getting HTTP 400 when I POST some JSON using RestSharp PCL. When I send a string, it seems that the \" is included. Which it should not. This might be the reason why the POST does not work.

I am probably missing something that I need to fill in but please do help me to understand what I am missing.

Here is the code I am using

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;
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Khiem-Kim Ho Xuan
  • 1,301
  • 1
  • 22
  • 45

1 Answers1

1

Have you checked this: How to POST request using RestSharp I know you are including the content type in the first argument but maybe you can play with RequestFormat? I doubt that's needed though. Also, have you checked whether your string does actually contain an escaped character like a double quote on it? If you are also seeing that slash on strings could it also be because you are debugging it? What do you receive in the payload coming through in the server that returns you the bad request?

Community
  • 1
  • 1
Carlos Torrecillas
  • 4,965
  • 7
  • 38
  • 69
  • Restsharp portable doesn't have requestformat though. The server includes the double quote. When I get the string back it gives me a string with quote in it. – Khiem-Kim Ho Xuan Dec 26 '15 at 13:32
  • have you tried this then: http://stackoverflow.com/questions/13833900/return-json-but-it-includes-backward-slashes-which-i-dont-want I'm assuming you have a WebAPI backend – Carlos Torrecillas Dec 26 '15 at 13:39
  • is it only failing on string because perhaps you are trying to convert to json something that is not in the right format i.e.: you have a string that has not a key-value pair format? – Carlos Torrecillas Dec 26 '15 at 14:57
  • yeah the backend part does not receive a json but a string at that part. But in general, when I pass a json in the same way. I still get an error, (another rest call). (which is what I was asking for). – Khiem-Kim Ho Xuan Dec 26 '15 at 15:13
  • Have you seen this one? http://stackoverflow.com/questions/33508073/bad-request-when-trying-to-add-json-body-to-request-with-restsharp . They are suggesting adding the "content-length" or using the AddJsonBody method (if available) – Carlos Torrecillas Dec 26 '15 at 15:19
  • Back to my problem. It doesn't seem to work for me at all. I use Restsharp Portable. Its a little bit different then what you are giving to me – Khiem-Kim Ho Xuan Dec 26 '15 at 15:40