skip to Main Content

I’m using RestSharp to consume a PHP based rest API (magento). I’m running into an issue where my request.Content contains a string escaped with backslashes. Like this: ""mystringIsEscaped"". It should just be a normal string "mystringIsNotEscaped".

The API does not give double quotes any specific treatment. A JSON response looks like this:

{
 "value": 1
}

OR

"SomeValueAsJustString"

Here is my code so far:

// PART 1: Getting an Unauthorized Request Token
var request = new RestRequest("/rest/V1/integration/admin/token", Method.POST);
request.AddJsonBody(new {username = "this.adminUserName", password = "this.adminPassword"});
request.AddHeader("content-type", "application/json");

var _client = new RestClient(_url);
var _jsonSerializer = new JsonSerializer();

_client.AddDefaultHeader(_contentTypeHeaderWithUnderscore ? "Content_Type" : "Content-Type", "application/json");
_client.ClearHandlers(); // http://stackoverflow.com/questions/22229393/why-is-restsharp-addheaderaccept-application-json-to-a-list-of-item
_client.AddHandler("application/json", _jsonSerializer);

var response = _client.Execute(request);

response.Content is double escaped.

My raw response (using fiddler) looks like this:

HTTP/1.1 200 OK
Server: nginx/1.17.1
Date: Mon, 22 Jul 2019 12:38:44 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Set-Cookie: PHPSESSID=873l8qhalaltets0tpa2s2sta1; expires=Mon, 22-Jul-2019 13:38:44 GMT; Max-Age=3600; path=/; domain=dev.myurlthatimhiding.org; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Vary: Accept-Encoding
Content-Length: 34

"mysecuretokenstringthatimnotshowing"

How do I get RestSharp to not double encode, or more specifically, to properly handle the double quotes returned?

2

Answers


  1. The response is generated on the PHP side. And as you can see with fiddler, the double quotes are coming from the PHP side and are not inserted by RestSharp.

    The content type is the response generated by the PHP service is:

    Content-Type: application/json; charset=utf-8
    

    So it’s supposed to be JSON.

    Usually, JSON data is longer and structured. But it’s also valid to return just a string. And in JSON, a string must be enclosed in double quotes.

    The back slashes are an artifact of the debugger. I assume you are using Visual Studio. It displays all strings the way the would appear in your source code. In source code you would write:

    var t = ""mysecuretokenstringthatimnotshowing"";
    

    So the back slashes aren’t contained, neither in the response nor in the string. It’s just the debugger display.

    It’s a strange use of JSON. But it’s valid. Just remove the double quotes at the beginning and at the end of the string and your fine.

    Login or Signup to reply.
  2. Looks like Execute() gives you content as a string, which would be with the double quotes. To deserialize, you need to provide a type:

    var response = request.Execute<string>();
    var token = response.Content;
    

    link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search