Remove Header and Test Again Postman
Postman is a tool to help you develop APIs. Postman helps y'all build APIs by providing tools to capture, validate, and test requests and responses. API testing is the process of verifying that your Application Programming Interface (API) is working correctly. This article will use Postman & Javascript for API testing.
Table of Contents
- Core concepts in Postman
- Transport HTTP requests with Postman
- Example Postman tests in Javascript
- Test API condition code using Postman
- Postman examination to check whether status is 200 OK
- Postman test to check whether status is 200 or 201
- Postman test to check status code is not 404
- Postman test to check status is not 404 or 500
- Test API response time using Postman
- Postman examination to check response time
- Test API response trunk using Postman
- Postman test to bank check field value in response
- Postman test to check nested field value in response
- Postman examination to cheque nested assortment value in response
- Test API response headers using Postman
- Postman examination to check response header
- Apply Postman Dynamic Variables to generate random data
- No code API testing using Testfully
Looking for a Postman alternative?
Testfully's complimentary plan offers Unlimited team members, Multi-footstep tests, team collaboration, historical test results and many more features. Import from Postman supported!
Cadre concepts in Postman
Postman offers many features, though; in this article, we will talk about how to test your API. The features include making requests, Inspecting responses, embedding global and Environment variables, and writing tests in Javascript, so without farther ado, let'southward showtime with some core concepts of Postman.
Postman Workspaces
A Postman workspace is where you lot can organize your API and team up with others in your arrangement.
At that place are three kinds of workspaces in Postman:
- Personal workspace, as the name suggests, is for personal usage.
- Individual workspace is simply available to people who yous invite to interact within the workspace. Then, you can organize information technology into folders and share it with your workspace members.
- Public Workspaces allow you lot to share your APIs with the world. They are searchable and accessible for free.
Postman's API testing features are available for Personal, Private, and Public workspaces.
Postman Collection
A Postman collection consists of a group of HTTP requests. As the name suggests, collections assistance y'all organize your workspace.
Collections offer features to collaborate with the team members, generate tests for your API, run the requests automatically, potency config, pre-request scripts, and any variables you desire to share amongst the collection's requests.
Request
Postman's requests are instructions for Postman to transport HTTP requests to any API. Requests are divers and configured by you using the Postman GUI.
The chances are that you don't take an API handy to endeavor out Postman with information technology. If that's the example, y'all can utilise Rick & Morty API or HTTP Bin API. For more information almost the available endpoints, delight consult the documentation for each API.
Ship HTTP requests with Postman
With the cadre concepts out of our mode, let'due south use Postman for sending a basic GET request.
Send a GET request with Postman
Typically, nosotros employ GET requests for retrieving data from an API. Follow the below steps to make your first request in Postman.
- Add a new request by hovering your mouse over your collection of selection (if you don't have whatever, please create ane now), click on the piffling
...icon, and so click on theAdd requestlink. - Selection a name that suits your asking best.
- Since the Get method is selected past default, yous don't need to select the method.
- Employ the
URLfield to set the endpoint URL - Click on the
Shippush. Postman sends the request to the provided endpoint and shows the result.
Postman displays the API response in the Response department.
Send a POST request with Postman
Unlike GET requests, POST requests can contain a request body. Postman allows you to include data of different formats in the asking trunk.
How to send Mail requests with Form Data in Postman
- Create a new request
- Select
POSTHTTP method from the dropdown. - Set your API endpoint in the
URLfield. - Click on the
Bodytab to set the asking body - Click on
class-data - Set the field name using the
Centralcolumn. - Set the field value using the
VALUEcolumn. - Click on the
Sendpush button.
Send a DELETE asking with Postman
In Restfuly APIs, Delete requests are responsible for deleting data. This step-past-stride guide lets you send aDELETE request to a selected API endpoint.
- Create a new request
- Select the
DELETEHTTP method from the dropdown. - Use the
Urlfield to fix the URL endpoint - Click on the
Sendbutton.
How to set query string parameters in Postman?
A query cord is a string of characters added to the end of a URL in a spider web browser to pass information to the API. Query cord could be anything from information almost the user (their location, age, interests, name) to information about the endpoint they are requesting).
A typical URL with a query string looks like http://httpbin.com/?anything=exam. A pair of key and value are separated using =.
We tin can use query string parameters with any HTTP method.
Here'due south how you can add together a query string parameter to your API endpoint URL in Postman:
- Set the query cord parameter name using the
KEYcolumn. - Gear up the query cord parameter value using the
VALUEcolumn. - Click on the
Sendbutton.
What are Variables in Postman?
Variables represent data and values in Postman. Yous can define a variable and reuse it by referencing it throughout your scripts and requests. Postman variables support different scopes. These scopes are:
- Global: these variables are accessible across your workspace.
- Collection: these variables are accessible in collection requests and contained of whatever environment.
- Environment: these variables are merely accessible when their corresponding surroundings is selected.
- Data: it can be imported from exterior sources, for example, from a JSON file.
- Local: every bit its name indicates, they are temporary and only accessible in your asking or script.
What are the Environment Variables, and how to use them?
Environments are a grouping of variables that you can use in your requests. It helps you manage the team fellow member'due south access to the shared data.
Creating a new environment, follow the steps listed below.
- Switch to the
Environmentstab and click on the+push - Choice a name for your environment
- Use the
VARIABLEcolumn to ready a proper noun for your environs variable - Use the
Electric current VALUEcolumn to set up a value - Once you are washed, click on the
Relievebutton
Case Postman tests in Javascript
Postman uses Javascript for API testing & API monitoring. If you don't know how to lawmaking in Javascript or prefer a no-code API testing tool, we highly recommend reading our top API testing tools commodity to acquire more about No Lawmaking API testing tools.
Examination API status code using Postman
If a user wants to interact with the API, they will send an HTTP asking to the API endpoint, and depending on the action and content of the request, the server responds with the appropriate status code. We tin define an HTTP response as either a success or an error depending on whether or non the request was successful.
Checking the response status code is one way to test an API. The beneath code snippets verify the HTTP response status lawmaking in Postman.
The table below lists pm fields with information related to the response condition code:
| Field | Description | Case |
|---|---|---|
pm.response.status | Status Text | OK |
pm.response.code | Status Code | 200 |
Postman test to check whether status is 200 OK
pm.test("the endpoint returns the expected status code", () => { // change 200 to the response lawmaking yous wait const expectedStatusCode = 200; pm.response.to.take.status(expectedStatusCode); }); Postman test to bank check whether status is 200 or 201
pm.test("the endpoint returns the expected status code", () => { // comma separate the valid response codes below const expectedStatusCodes = [200, 201]; pm.wait(pm.response.code).to.exist.oneOf( expectedStatusCodes, `expected response condition to exist one of ${ expectedStatusCodes } just got ${ pm.response.lawmaking } .` ); }); Postman test to check condition code is not 404
pm.test("the endpoint does not return unexpected status code", () => { // change 404 to the response code you practice not wait const expectedStatusCode = 404; pm.response.to.not.have.status(expectedStatusCode); }); Postman test to check status is not 404 or 500
pm.test("the endpoint does not return unexpected status codes", () => { // comma separate the valid response codes below const unexpectedStatusCodes = [404, 500]; pm.look(pm.response.code).to.not.be.oneOf( unexpectedStatusCodes, `did non expect response condition to be one of ${ unexpectedStatusCodes } but got ${ pm.response.code } .` ); }); Examination API response time using Postman
The API response time is an of import metric to exam, measuring how long it takes for the API to answer to requests. Ho-hum response times tin can pb to poor user experience and exist severely affected by summit traffic atmospheric condition. This section shows y'all how to test response time in Postman.
The table below lists pm fields with data related to response time:
| Field | Description | Example |
|---|---|---|
pm.response.responseTime | Response time (in milliseconds) | 255 |
Postman exam to check response time
pm.test("API responds within the expected treshhold", () => { // fix the response time in milliseconds const expectedTimeInMilliseconds = 500; pm.expect(pm.response.responseTime).to.be.lessThan( expectedTimeInMilliseconds + 1, `The endpoint did non reply inside ${ expectedTimeInMilliseconds } ms. Response came in ${ pm.response.responseTime } ms` ); }); Test API response body using Postman
API response has a trunk, which is the operation's return value. The response trunk is returned in a JSON- or XML-encoded string. The response body defines the structure and content of the response payload.
The tabular array below lists pm fields and functions with information related to the response body:
| Field / Part | Description | Case |
|---|---|---|
pm.response.json() | Response body (Javascript object) | {"hello": "world"} |
pm.response.body | Response body (text) | {"hello": "globe"} |
Postman test to cheque field value in response
We can validate the value of both id and name fields of the https://rickandmortyapi.com/api/character/2 using the test below.
pm.exam("API response contains the expected fields", () => { const response = pm.response.json(); // the line below checks value of the id field is two (number). pm.expect(response).to.have.belongings("id", two); // the line below checks value of the name field is Morty Smith (string). pm.look(response).to.have.holding("proper name", "Morty Smith"); }); Postman test to cheque nested field value in response
The script below step works for fields at the root of the response. What if we wanted to examination the name field nether the origin field. We tin can tweak the script to support fields at any level.
pm.examination("API response contains the expected fields", () => { const response = pm.response.json(); // the line beneath checks value of the id field is 2 (number). pm.expect(response).to.have.nested.property("id", ii); // the line below checks value of the name field is Morty Smith (string). pm.await(response).to.have.nested.holding("name", "Morty Smith"); // the line below checks value of the origin.proper name field is Globe (C-137) (cord). pm.look(response).to.take.nested.property("origin.proper name", "Globe (C-137)"); }); To check value of nested fields, provide the path (from root) to the field by chaining field names using dot (.)
Postman test to check nested array value in response
We can take it even farther and use the same technique to validate the value of items in the array. For case, we tin use the below script to bank check the value of the second item in the episode array of the https://rickandmortyapi.com/api/character/2 endpoint.
pm.exam("API response contains the expected fields", () => { const response = pm.response.json(); // the line beneath checks the value of the origin.name field is Earth (C-137) (string). pm.wait(response).to.have.nested.property("episode.ane", "https://rickandmortyapi.com/api/episode/2"); }); You may have noticed, but to access the second detail in the episode list, nosotros used the number 1; why? In Javascript, the position of the items in lists (array) starts from 0, then the offset item has 0 equally the position number, the second ane 1, then on.
When you hit an API endpoint, one or more HTTP header is returned, along with the data from the call. This header contains information well-nigh the API endpoint that was chosen and tin can be used past API clients to understand improve the call that was made.
The table below lists pm fields and functions with data related to response headers:
| Field | Description |
|---|---|
pm.response.headers | Response heaaders |
pm.test("API response contians the expected header", () => { pm.response.to.take.header("Content-Blazon", "application/json; charset=utf-8"); }); Apply Postman Dynamic Variables to generate random data
Including random data in the request is an excellent technique for API testing. In particular, including random data proves that the API is not biassed towards i particular form of data.
Dynamic Variables is a Postman API Testing feature yous tin leverage to include randomly generated imitation data of different types in the request payload. Postman takes care of generating fake values on the asking time. Dynamic Variables support ID, Email, Postal Lawmaking, and many more information shapes.
Include a Postman dynamic variable by typing
{{$in the request body field and then selection one of the available options.
Below is a listing of variables y'all can apply with Postman. Please check the Dynamic Variables article on the Postman website for a complete list.
| Dynamic Variable Proper name | Purpose | Example |
|---|---|---|
{{$guid}} | Produces a UUID V4 on the fly | 611c2e81-2ccb-42d8-9ddc-2d0bfa65c1b4 |
{{$randomBankAccountName}} | generates a random bank business relationship proper noun | Personal Loan |
{{$isoTimestamp}} | The current ISO timestamp at zero UTC | 2020-06-09T21:10:36.177Z |
{{$randomInt}} | A random integer betwixt 0 and 1000 | 123 |
{{$randomFirstName}} | A random showtime name | David |
{{$randomPhoneNumber}} | A random 10-digit phone number | 562-203-1827 |
The screenshot beneath demonstrates how to send a Post asking that includes randomly generated fake information for ID and depository financial institution account name.
Since yous're interested in this feature, nosotros highly recommend reading our article about fuzz testing. It is a type of automated software testing, a method of discovering bugs in software past providing random input to the software under the test and monitoring whatsoever crashes and failed assertions.
No code API testing using Testfully
Testfully offers a generous free plan
Testfully is a leading No Lawmaking API testing & monitoring tool and a great Postman alternative for API testing. The below video is a quick demo of Testfully and how you tin can utilize information technology to test your APIs without writing code.
Source: https://testfully.io/blog/postman-api-testing/
0 Response to "Remove Header and Test Again Postman"
Post a Comment