Skip to content

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:

ParameterTypeDescription
urlstringThe URL to send the HTTP request to.
Required if not passed as the first argument.
dataobjectRequest 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.
paramsobjectKey-value map for URL query parameters.
E.g. Given {product: 1, currency: ['USD', 'JPY']}, this sets the url search parameter to ?product=1&currency=USD&currency=JPY.
If you need custom array serialization, pass values as strings instead.
responseTypestringExpected 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.
headersobjectMap of HTTP request headers.
authobjectBasic authentication credentials. This should be a map containing values for username and password. Overrides the Authorization header.
withCredentialsbooleanDefaults 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:

ParameterTypeDescription
statusnumberHTTP status code, e.g. 200, 404, 500.
statusTextstringStatus text, e.g. "OK", "Not Found", "Internal Server Error".
responseTypestringResponse type, e.g. "json".
dataobjectResponse 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)