By using the Web API call command, you can run the Web API (REST API) in your test script. The Web API calls support calls to the HTTP GET / POST / PUT / DELETE methods.
Table of contents
- GET method
- POST/PUT/DELETE methods
- Specifying Status Code
- Note on setting “Content-Type” in the Header key
- Using Variables in Requests
- IP address when accessing URL
GET method
As an example, let us try accessing the RANDOM USER GENERATOR, which enables you to acquire different user information at random in JSON format, as below.
To simplify the explanation, we have removed a great deal of the information from the actual API response.
{
"results": [
{
"name": {
"first": "levi",
"last": "jones",
}
}
]
}
First, click the Edit button.
In the dialog, specify GET for Method and https://randomuser.me/api?format=json for API URL.
Furthermore, in the Result tab, a variable is set to the value of the API response so that it can be used in subsequent processing. In the Variable column, specify the variable name, and specify the response value in the Javascript column. In the Javascript column, you can reference the response as a JSON object with
- "jsonResponse" for accessing the response body
- "responseHeaders" for accessing the response headers
and each item can be accessed using the Javascript notation, such as “jsonResponse["results"]” and "responseHeaders["Set-Cookie"]".
Values set as variables can be referenced with $(variable name) in subsequent commands in the same test case. We can set the user's last name in variable LastName and their first name in variable FirstName, and this can then be used to set a random user name in the input area, as shown as below.
- The saved variables are only valid within the same test case. They cannot be used in other test cases.
- When you want to specify an HTTP request header, specify the Header tab in the dialog box.
- The query parameter of the GET method can be specified with the format “?<key 1>=<value 1>&<key 2>=...” after the URL.
POST/PUT/DELETE methods
The method for specifying the method name, URL, header, and response are the same as for the GET method.
Additionally, in the case of the POST / PUT / DELETE methods, you can specify the body of the request on the Body tab, as below. The body value can be either form-data format (POST method only) or raw data format (text format).
Specifying Status Code
In the Result tab, you can specify the expected HTTP response status code. This allows you to execute tests even when the API call returns an error response.
By default, the Web API call is considered successful if a success status such as 200 is returned. When you select Specified value(s), the Web API call is considered successful if the expected status code matches the actual response status.
In Specified value(s), you can enter multiple status codes or ranges separated by commas (e.g., 200-299, 429, 503). The Web API call is considered successful if the response status matches any of them.
You can also store the response status code in a variable by entering "statusCode" in the Javascript column of the Result tab.
Example: Repeat a Web API call until a specific status code is returned
Sometimes, an API endpoint may return a non-200 status code, such as 503 Service Unavailable, if it temporarily cannot process a request. In these cases, you can keep calling it until it returns 200 by combining it with the Repeat until variable equals command, as follows.
- Repeat until variable equals: variable "STATUS", string "200"
- Web API call: In the Result tab, enter "200, 503" in Specified value(s) for the expected status code, and set the variable "STATUS" with "statusCode" in the Javascript column.
- End repeat
Because 503 is included in the expected status codes, the step does not fail when 503 is returned, and the loop continues. You can adjust the interval between calls and the maximum duration with the timeout and interval settings of the repeat command (see Repeat Until command).
Note on setting “Content-Type” in the Header key
If the first letter of Content and Type are in lower case, it may fail with a 400 BadRequest error; please capitalize the first letter of Content and Type.
OK: Content-Type
NG: content-type, Content-type, content-Type
Using Variables in Requests
In Web API call requests, you can use variables for certain fields. Variables can be used in the following parts:
API URL
Header value
Body (form data) value
Body (raw data)
Please note that variables cannot be used in the key of headers or form data bodies.
IP address when accessing URL
- In a cloud environment: The access source IP address is the IP address that MagicPod cloud devices and browsers use when accessing external networks (see Test in private environments). This IP address is normally shared with other MagicPod users. If you use the user fixed IP feature available with the Enterprise Plan, a dedicated IP address for your organization is used, allowing you to set stricter IP restrictions.
- In a local PC environment: The IP address of the local PC is used as the access source. Therefore, WebAPI that is not public can be called as long as the local PC is accessible.