Deserialize JSON json URL for query parameters in Specflow not working

124 views Asked by At

How to encode query parameters on a url in Specflow? the GET Call for API with Search options with changing scenarios as Case with searchOperator used = equal, in, like

GET API call for url.com/?SearchOptions=[{"Searchby":"title","searchOperator":"contains","searchValue1":"testing"}]

stepdefination

 [When(@"Requesting for Search: (.*), (.*)")]
        public void WhenAGetRequestSearchOnly(string searchOption, int statusCode)
        {
            switch (statusCode)
            {
                case 401:
                    Token=invalidtoken";  - This pass
                case 200:
                    Token="invalidtoken"; -Failed 400 (def url is not encoded properly) 
                  break;
            }
            SetResponse(EndPoints.GetResponseSearchOnly(_Token, searchOption, statusCode));

        }

specflow - Same is coming in postman (which means url is not properly encoded)

Need url to hit - /endpointapi?SearchOptions=[{%22Searchby%22:%22title%22,%22searchOperator%22:%22contains%22,%22filterValue1%22:%22testing%22}]

API GET call works fine url.com/?SearchOptions=[{"Searchby":"title","searchOperator":"contains","searchValue1":"testing",searchValue2":""}] When endoded this url it comes like this

%7B%22SearchBy%22%3A%22title%22%2C%22SearchOperator%22%3A%22contains%22%2C%20%22SearchValue1%22%3A%22Testing%22%7D which is giving above error

As an Example browser url is seen working fine (pls ignore the values of json) url captured as an example

Note- used online json query encoder filter

2

There are 2 answers

0
GPs On BEST ANSWER

This has been resolved by using below reference

References:

string json = "[{"Searchby":"title","searchOperator":"contains","searchValue1":"testing"}]";
var stringified = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<dynamic>(json));
Console.WriteLine(stringified); 

Prints:

"[{\"Searchby\":\"title\",\"searchOperator\":\"contains\",\"searchValue1\":\"testing\"}]"
2
Ritesh Grover On

Try the below string:

%5B%7B%22Searchby%22%3A%22title%22%2C%22searchOperator%22%3A%22contains%22%2C%22searchValue1%22%3A%22testing%22%7D%5D

instead of:

[{"Searchby":"title","searchOperator":"contains","searchValue1":"testing"}]