title: RestSharp How to deal with different JSON response formats for same query
body: <p>I've been fighting with this for a while and I think is time to ask for some help.</p>
  
  <p>I'm using <strong>RestSharp</strong> to connect to my local <strong>PHP REST API</strong>. The format I'm returning the api results is as follows (JSON):</p>
  
  <pre><code>{
  "estado": "login_correcto",
  "content": {
     "id": 1,
     "nombreusuario": "daniaguado",
     "contrasena": "qwerty",
     "email": "daniel@zadecon.com",
     "nivelacceso": 5,
      "created_at": "2017-08-01 10:31:16",
      "updated_at": "-0001-11-30 00:00:00"
      }
  }
  </code></pre>
  
  <p>For this, I've created a custom <code>Usuario</code> class (User) and a custom <code>ResponseUsuario</code> as follows:</p>
  
  <pre><code>class Usuario
  {
      public int id { get; set; }
      public string nombreusuario { get; set; }
      public string contrasena { get; set; }
      public string email { get; set; }
      public int nivelacceso { get; set; }
      public string created_at { get; set; }
      public string updated_at { get; set; }
  }
  </code></pre>
  
  <p>Then the ResponseUsuario:</p>
  
  <pre><code>class ResponseUsuario
  {
      public string estado { get; set; }
      public Usuario content { get; set; }
  }
  </code></pre>
  
  <p>The response is being parsed ok when the response is ok (202) and the user exists.</p>
  
  <p>But, when the login is incorrect, in "content" I'm returning a message, not an User:</p>
  
  <pre><code>{
   "estado": "login_incorrecto",
   "content": "La contraseña es incorrecta / Password incorrect"
  }
  </code></pre>
  
  <p>So, If I use <code>ResponseUsuario</code> there, queryResult.Data is null, because it cannot map the content to the <code>Usuario</code> class. The fact is that if I don't use ResponseUsuario and instead use a <code>Response</code> class, in which content variable is type object, I cannot cast it to <code>Usuario</code> and I cannot deal with it.</p>
  
  <p>This is the more general <code>Response</code> class which I understand I should be using for all my queries:</p>
  
  <pre><code>class Response
  {
      public string estado { get; set; }
      public object content { get; set; }
  }
  </code></pre>
  
  <p>Finally, this is my RestSharp query:</p>
  
  <pre><code>ApiClient api = new ApiClient(); // Just create the client with some common parameters
  
  var request = new RestRequest("usuarios/login", RestSharp.Method.POST);
  
  request.AddParameter("nombreusuario", textbox_usuario.Text);
  request.AddParameter("contrasena", textbox_contrasena.Text);
  
  var queryResult = api.cliente.Execute&lt;ResponseUsuario&gt;(request);
  
  if (queryResult.StatusCode == System.Net.HttpStatusCode.OK)
  {
      Usuario u = (Usuario) queryResult.Data.content; // Gives error when using Execute&lt;Response&gt; (object content)
      String s = queryResult.Data.estado;
  
      Console.Out.WriteLineAsync($"OK - {u.nombreusuario} - {u.email} - Acceso: {u.nivelacceso}");
  } 
  else
  {
      Console.Out.WriteLineAsync($"Query Failed: {queryResult.StatusDescription}, estado: {queryResult.Data.estado}"); // Giving error when using Execute&lt;ResponseUsuario&gt;
  }
  </code></pre>
  
  <p>How can I fix the cast to Usuario (or the matching class for content)? I think the <code>Response</code> class should be common to all my queries to the API and then cast the Content part to it's appropiate class, but I don't know how to do it.</p>