Skip to content

Making HTTP Requests ​

An HTTP request is a message sent from the browser to a server to request data or perform an action.

UI-licious lets you perform HTTP requests directly in the browser context, automatically including cookies for the current page’s domain. This is useful for retrieving test data or setting up application state during UI tests.

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.

Need custom headers on browser page navigations and assets?

UI.httpGet(), UI.httpPost(), and UI.httpRequest() execute isolated API requests directly in JavaScript. If you need custom HTTP headers (such as Authorization or bot protection tokens) attached automatically to all browser navigations and network traffic, see the Global HTTP Request Headers Guide.

Perform an HTTP Get ​

To perform an HTTP GET request in the browser’s context, use UI.httpGet().

js
// go to the pet store
I.goTo("https://petstore.swagger.io")

// get a pet using its ID 
let res = UI.httpGet("https://petstore.swagger.io/v2/pet/9216678377732869000")

// log the response data 
if(res.status === 404){
    TEST.log.fail("Pet not found") 
} else {
    TEST.log.info("Response from API is: ", res.data) 
}

Use UI.httpGet() to retrieve data from an API endpoint without modifying server state.

To learn more about the other options, you can refer to this guide

Perform an HTTP Post ​

To perform an HTTP POST request in the browser’s context, use UI.httpPost().

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)

Use UI.httpPost() when you need to send data to the server, such as creating or updating records.

To learn more about the other options, you can refer to this guide

Perform an HTTP Request ​

For more control over the HTTP method and request options, use UI.httpRequest().

js
// go to the pet store
I.goTo("https://petstore.swagger.io")

// update pet "0"
let res = UI.httpRequest({
		url : "https://petstore.swagger.io/v2/pet",
		method: "POST",
        data: {
            "id": 0,
            "name": "doggie",
            "status": "available"
        },
        responseType: "json", 
    })

// log the response from the API
TEST.log.info("Response from API is: ", res.data)

UI.httpRequest() allows you to specify the HTTP method, request body, and response handling in a single call.

To learn more about the other options, you can refer to this guide