Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fetch complete Json string from Retrieve method when no root element is provided #25

Open
k070080 opened this issue Sep 12, 2017 · 3 comments

Comments

@k070080
Copy link

k070080 commented Sep 12, 2017

Suppose we have response json like below, we want to get complete json to deserialize into dictionary(collection) inorder to verify from database but .Retrieve(x=> x) is crashing.

{
"customerId": "12345",
"title": "GitHub",
"Address": {
"City": "NYC",
"Zip": "11223344"
},
"alias": {
"type": "ContactNumber",
"value": "12345678910"
},
"Membership": "Individual"
}

@amar-1314
Copy link

Can someone please look into it?

@ViniGasp
Copy link

I have the same problem, I need the response body to perform validations with the database.
I'm working on a big project and this is essential for validations.

Thanks.

@numo16
Copy link

numo16 commented May 12, 2020

The lack of strongly typed Retrieve can be gotten around with something like JSON.net to serialize/deserialize to your desired type. I use an extension method like the following to wrap this functionality:

public static T RetrieveBody<T>(this ResponseContext context)
{
    var json = (string)context.Retrieve(x => JsonConvert.SerializeObject(x));
    return JsonConvert.DeserializeObject<T>(json);
}

Which then allows you to use it like this:

var response = new RestAssured()
  .Given()
    .Name("JsonIP Test Suite")
    .Header("Content-Type", "application/json")
    .Header("Accept-Encoding", "gzip,deflate")
  .When()
    .Get("http://jsonip.com")
  .Then()
    .RetrieveBody<Dictionary<string,string>>();

Assert.Equal(response["ip"], "2605:6000:f705:ab00:78e3:1959:78d4:bd76");

where Dictionary<string, string> is whatever type you want to convert the response to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants