How To Write Header Parameters In Rest Web Service Request
And so far in this series, we have covered reading the response body . But equally you keep ahead, a unproblematic GET or a POST asking won't suffice to the complexity your test scenario might have. Y'all may need to test the API for dissimilar query parameters. In this article, we volition comprehend how query parameters are passed in the HTTP asking using remainder bodacious with the post-obit chief sections-
- What is the composition of the URL?
- What are Query Parameters?
- How to send a asking using Query Parameters in Rest Assured?
What is the composition of the URL?
Similar to the examples used in the previous articles nosotros will be using the Bookstore API in this article as well. Consider the below sample URL to fetch details respective to a book-
https://demoqa.com/BookStore/v1/Volume?ISBN=9781449325862 Though the above URL looks very basic, it still let united states of america understand its different components.
- https:// - It is a protocol that ensures secure connexion betwixt the web server and the web browser.
- https://demoqa.com/swagger/#/BookStore - It is the domain name that hosts the website. It generally ends with .com, .in, .net, etc.
- BookStore/v1/Book - This is the path or the URI that identifies the resources applied past the asking.
- ?ISBN=9781449325862 - This is a string query parameter. The question mark denotes the start of a query string parameter. There can be one or more than query parameters in the URL.
Permit united states of america now empathise more about query parameters in the next section.
What are Query Cord Parameters?
Yous might not ever want to fetch all the results corresponding to a request. There may be scenarios where you need to fetch only a few or a unmarried tape. In such cases, query string parameters play an of import role. These are appended at the end of the URL later using a '?'. Try entering the instance URL in the browser address bar and observe the results available under Network -> Payload-
The query parameter, i.e., ISBN=9781449325862, is displayed under the Query String Parameters field. In a similar fashion, if in that location are multiple query parameters passed in the URL, yous will see all of them in this field. As an exercise, you may search some keywords on Google search and run into the query parameters that get appended to the URL. Find the search keyword is displayed subsequently 'q=your search keyword'. Additionally, you lot will find other query parameters which are separated from each other through an '&' sign.
These signs are termed URL parameters and are across the scope of our discussion here.
Now that we take understood what query parameters are and how they are used in requests, we tin can proceed to see how nosotros tin can send requests with Query Parameters in Rest Assured.
How to send a request using Query Parameters in Rest Assured?
In this department, we will meet how nosotros tin automate our test case by passing query parameters in Rest bodacious. We will but use the URL containing the query parameter every bit discussed in sections above to bear witness how we tin can send a request using query parameters in rest assured. Allow us encounter how the lawmaking would await similar and then we volition walkthrough information technology pace by step.
package bookstore; import org.junit.Test; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; import io.restassured.response.ResponseBody; import io.restassured.specification.RequestSpecification; public form QueryParam { @Test public void queryParameter () { //Defining the base URI RestAssured.baseURI= "https://bookstore.toolsqa.com/BookStore/v1"; RequestSpecification httpRequest = RestAssured.given(); //Passing the resource details Response res = httpRequest.queryParam("ISBN","9781449325862").get("/Volume"); //Retrieving the response trunk using getBody() method ResponseBody torso = res.body(); //Converting the response body to string object Cord rbdy = body.asString(); //Creating object of JsonPath and passing the string response trunk every bit parameter JsonPath jpath = new JsonPath(rbdy); //Storing publisher name in a string variable String championship = jpath.getString("title"); Arrangement.out.println("The book championship is - "+title); } }
RestAssured.baseURI= "https://bookstore.toolsqa.com/BookStore/v1"; RequestSpecification httpRequest = RestAssured.given();
We first define the base URI to create a asking to the service endpoint.
Response res = httpRequest.queryParam("ISBN","9781449325862").become("/Volume");
Next we transport the resource details like the book ISBN as a query parameter to search in books using the Become request.
Note: If you need to send multiple query parameters y'all just demand to append queryParam() method with the parameter proper noun and value to the RequestSpecification object, i.due east. httpRequest in the above instance.
ResponseBody body = res.body(); String rbdy = trunk.asString(); JsonPath jpath = new JsonPath(rbdy); Cord title = jpath.getString("title"); Arrangement.out.println("The volume title is - "+title);
Finally, we are storing the response body as a String object and parsing its unlike values using the JSONPath object. We then fetch the title from the response body.
Notation: You may read more than about JSONPath in our elaborate article on the same.
And that'due south information technology! See how easily nosotros could ship the query parameters to our request by simply using a queryParam() method. The higher up lawmaking execution displays the championship of the book with respect to the ISBN.
Y'all can now get ahead with automating your test scripts with request having query parameter(s) and enhance your rest assured code.
Cardinal Takeaways
- Nosotros have seen the different components of a URL and the purpose of each component.
- We also understood what are query parameters and how they work when nosotros access a URL in the browser.
- Query parameters passed to the rest assured tests using the queryParam() method accepts the parameter proper name and value.
- You lot may use the queryParam() method not just once, just as many times as the number of query parameters in your Go request.
How To Write Header Parameters In Rest Web Service Request,
Source: https://www.toolsqa.com/rest-assured/query-parameters-in-rest-assured/
Posted by: alexanderaunce1959.blogspot.com
0 Response to "How To Write Header Parameters In Rest Web Service Request"
Post a Comment