samedi 6 février 2021

Get value from JSON ResrSharp

I just started with testing API and have a problem with get data from response. In postman I got this data when I test GET method: { "Data": { "Id": 1, "Role": "Admin", "Status": "Active", "Username": "Marta", "FirstName": "First", "LastName": "Admin", "PhoneNumber": "", "Email": "" }, "Success": true }

Below I wrote test in restSharp for get method, but I can not get specific key:value.

My test method bellow:

public void TestGet()
        {
            var client= new RestClient("http://localhost:8080/");

            var request = new RestRequest("user/{postid}", Method.GET);

            request.AddUrlSegment("postid", 1);

            request.AddHeader("Accept", "application/json");
            request.AddHeader("Authorization", "TWFydGE=:OTEwNjEyU3pjemVjaW4h");
            request.AddHeader("Content-Type", "application/json");

            

            var response = client.Execute(request);           

            //one way I tried, but not work 
            //var deserealize = new JsonDeserializer();
            //var output = deserealize.Deserialize<Dictionary<string, string>>(response);
            //var resolt = output["Username"];

            //Assert.That(resolt, Is.EqualTo("Marta"));


            //seconde way by Newtonssoft

            JObject obs = JObject.Parse(response.Content);         

            Assert.That(obs["Username"].ToString(), Is.EqualTo("Marta"));

I tried to reproduce the problem, but this time I used a json server instead of the application and when I change the JSON format so that the key is small, I can get to the data.

{ "data": { "id": 1, "role": "Admin", "status": "Active", "username": "Marta", "firstName": "First", "lastName": "Admin", "phoneNumber": "", "email": "" }, "Success": true }

Please let mi know where I did mistake or this is framework problem ?

Aucun commentaire:

Enregistrer un commentaire