1

I'm trying to get a specific value from my content, but I don't have any idea how I can do it.

I'm using RestSharp(C#) to run my JSON code, but when I execute the command it returns an error. I need to get the value from the property errorMessage.

var json = JsonConvert.SerializeObject(objectToUpdate);
var request = new RestRequest(Method.POST);

request.RequestFormat = DataFormat.Json;
request.AddCookie("authToken", token);
request.AddParameter("application/json", json, ParameterType.RequestBody);

var response = client.Execute<T>(request);

After execute this code my response return the below JSON:

{
    "response":{},
    "status":{
        "success":false,
        "detail":{
            "errormessage":[{
                "fieldName":"departmentCode",
                "errorMessage":"Department code provided is already associated with another department",
                "errorCode":"DUPLICATE_DEPARTMENT_CODE"
            }],
            "error":"Validation failure on request",
            "operation":"internal",
            "errorcode":"412"
        }
    }
}
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
  • try "request.Parameters.Clear();" before "request.AddCookie("authToken", token);" – Darko Maric Programer Nov 03 '17 at 14:47
  • might take a look https://stackoverflow.com/questions/11400879/how-to-post-request-using-restsharp . Other for that - error message shows that your message fails validation - you have content issue in departmentCode – Vladimir Nov 03 '17 at 14:47
  • Hello @DarkoMaricProgramer, I don't have problems with parameters, I just need to get a information from **errorMessage** to save in my log. – Evandro Sousa Nov 03 '17 at 14:49
  • I see...then try:dynamic result = JObject.Parse(response); var detail = result.status.detail.errormessage; foreach (var item in detail){}; – Darko Maric Programer Nov 03 '17 at 14:53

1 Answers1

0

You could have a class, that represents the response you expect:

class ApiResponse{
    // use a class that represents normal response instead of object
    // if you need to interact with it.
    public object Response {get; set;}
    public ResponseStatus Status{get; set;}
}
class ResponseStatus {
    public StatusDetail Detail{get; set;}
    public bool Success {get; set;}
}
class StatusDetail {
    public ErrorMessage[] ErrorMessage{get; set;}
}
class ErrorMessage{
    public string FieldName{get; set;}
    public string ErrorMessage{get; set;}
    public string ErrorCode{get; set;}
}

Then you have that, you can get parsed response from RestSharp client:

var response = client.Execute<ApiResponse>(request);
var message = response.Data.Response.Detail.ErrorMessage.First().ErrorMessage;

According to RestSharp docs, they recomment using response classes over dynamic objects (https://github.com/restsharp/RestSharp/wiki/Recommended-Usage).

  • 1
    Thank you everyone. I used the exemple that Justinas said and... PUFFF.... I builded the structure of classes and the code worked.... ApiResponse apiResponse = new ApiResponse(); apiResponse.Response = JsonConvert.DeserializeObject(JObject.Parse(response.Content)["status"].ToString()); – Evandro Sousa Nov 03 '17 at 16:09