Appearance
UI.httpPost()
Perform an HTTP POST request in the browser's context.
Because the request is executed in the browser, any cookies associated with the current page's domain are automatically included. This is useful for scenarios such as making authenticated requests after a user logs in.
If the HTTP response status code is 400 or above, the command fails and the test is marked as failed. To ignore such errors in the test, use UI.httpGet$ instead.
INFO
Performing HTTP requests can be useful for retrieving test data or setting up application state during UI tests. We do not recommend using these commands for API testing — instead use dedicated tools that are efficient and purpose-built.
Usage
js
var response = UI.httpPost(url: string, options: object)
var response = UI.httpPost(options: object)Parameters
The options object includes the following configuration properties:
| Parameter | Type | Description |
|---|---|---|
| url | string | The URL to send the HTTP request to. Required if not passed as the first argument. |
| data | object | Request body to send. If data is a JSON object, and Content-Type header is not specified, the header will be automatically set to application/json;charset=utf-8. |
| params | object | Key-value map for URL query parameters. E.g. Given {product: 1, currency: ['USD', 'JPY']}, this sets the url search parameter to ?product=1¤cy=USD¤cy=JPY.If you need custom array serialization, pass values as strings instead. |
| responseType | string | Expected response type. By default, the response's data object will contain the raw response body as a string.If set to "json", the response's data object will be automatically parsed to JSON. |
| headers | object | Map of HTTP request headers. |
| auth | object | Basic authentication credentials. This should be a map containing values for username and password. Overrides the Authorization header. |
| withCredentials | boolean | Defaults to true. Allows requests to include credentials such as cookies and authorization headers. Setting this to false may result in CORS or network errors, depending on the browser. |
Returns
An object with the following contents:
| Parameter | Type | Description |
|---|---|---|
| status | number | HTTP status code, e.g. 200, 404, 500. |
| statusText | string | Status text, e.g. "OK", "Not Found", "Internal Server Error". |
| responseType | string | Response type, e.g. "json". |
| data | object | Response body. If responseType is "json", this is automatically parsed into JSON object. Otherwise, this is a string containing the raw response body. |
Example
js
// go to the pet store
I.goTo("https://petstore.swagger.io")
// update pet "0"
let res = UI.httpPost("https://petstore.swagger.io/v2/pet", {
data: {
"id": 0,
"name": "doggie",
"status": "available"
},
responseType: "json",
})
// log the response from the API
TEST.log.info("Response from API is: ", res.data)