0

I am working on a side project at work (I'm not a developer).

What I'm trying to achieve is validating whether statusis NULL or it has ERR in the login request response so I can know if I may continue the process.

This is the code I have so far:

namespace EseyeSIMAPI
{
class Program
{
    static void Main(string[] args)
    {
        var client = new RestClient("https://siam.eseye.com/Japi/Tigrillo");
        var loginRequest = new RestRequest("/login/", Method.POST);

        loginRequest.AddHeader("postman-token", "4e67ed4c-4130-4067-9539-d5ed6b6ad761");
        loginRequest.AddHeader("cache-control", "no-cache");
        loginRequest.AddHeader("content-type", "application/json");
        loginRequest.AddParameter("application/json", "{\r\n\"username\" : \"someusername\" ,\r\n\"password\" : \"somepassword\" ,\r\n\"portfolioID\" : \"someportfolioid\"\r\n}", ParameterType.RequestBody);

        IRestResponse response = client.Execute(loginRequest);

        client.ExecuteAsync(loginRequest, x =>
        {
            var json = x.Content;
        });

        Console.ReadLine();
    }
}
public class LoginStatus
{
    public Status status { get; set; }
    public string cookie { get; set; }
    public string permissions { get; set; }
    public string canActivate { get; set; }
}

public class Status
{
    public string errorCode { get; set; }
    public string errorMessage { get; set; }
    public string status { get; set; }
}
}

I receive the following JSON response:

{
"status": {
"errorCode": "",
"errorMessage": "",
"status": "OK"
},
"cookie": "2p255ju6q1lfql594uppnq9lp2",
"permissions": "ULE",
"canActivate": "yes"
}

So I created a class that will handle all the response parameters in the JSON object. I'm just not sure how exactly to access status.

4
  • So you want to de-serialise x.Conent to LoginStatus is that correct?
    – musefan
    Feb 23, 2017 at 11:28
  • If so, then this question seems to have a suitable answer
    – musefan
    Feb 23, 2017 at 11:30
  • @musefan yep. I want to be able to access each key-value pair.
    – Ko Ga
    Feb 23, 2017 at 11:31
  • If you have an alternate solution that solves your problem, post that as a new answer. Don't edit the question to include the solution
    – musefan
    Feb 23, 2017 at 14:00

1 Answer 1

2

Following on from my comments, this is how you would specifically do it with your code using the JavaScriptSerializer class:

client.ExecuteAsync(loginRequest, x =>
{
    var json = x.Content;

    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
    LoginStatus loginStatus = jsonSerializer.Deserialize<LoginStatus>(json);

    string errorCode = loginStatus.status.errorCode;
});
6
  • I'm getting the following error: Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'EseyeSIMAPI.LoginStatus'.
    – Ko Ga
    Feb 23, 2017 at 11:40
  • @KobbiGal: Sorry, I don't have ability to test at the minute. Can you try my updated answer
    – musefan
    Feb 23, 2017 at 11:53
  • Error CS0308 The non-generic method 'JavaScriptSerializer.DeserializeObject(string)' cannot be used with type arguments
    – Ko Ga
    Feb 23, 2017 at 11:58
  • 1
    @KobbiGal: OK, I fixed it now. And tested it this time too. It definitely works now.
    – musefan
    Feb 23, 2017 at 12:13
  • How come when I enter wrong credentials, printing the errorcode still returns nothing? (it's supposed to return E0000)
    – Ko Ga
    Feb 23, 2017 at 13:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.