UI.httpPost

Perform a HTTP POST request.

Usage

var response = UI.httpPost(url)
var response = UI.httpPost(url, options)
Parameter Type Remarks
url string The url to make a request to.
options object Optional. Options to configure the HTTP request. See "options" parameter table below.

The options object

Parameter Type Remarks
data object The request data to send. Ignored for GET and HEAD methods.
If data is a json object, and content-type header is not specified, the content-type will automatically default to application/json;charset=utf-8
params object Key-value map to set the url search 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 arrays to be serialised differently, you will need to pass the values as a string literal instead of an array.
responseType string The 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 request headers to set.
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. If true, allows cross-site requests to be made using credentials such as cookies, Authorization headers or TLS client certificates, and allows cookies from response to be set. If this is false, you might get CORS or Network errors (reported error may be different across browsers) when making cross-site requests.

Returned object

Parameter Type Remarks
status number The status code, e.g. 200, 404, 500
statusText string The status text, e.g. "OK", "Not Found", "Internal Server Error"
responseType string The response type, e.g. "json"
data object The response body. If responseType is "json", this is automatically parsed into JSON object. Otherwise, this is a string containing the raw response body.

Assertion behaviors

If the response status code is less than 400, the assertions for these commands will pass. Otherwise, the assertions for these commands will fail, and you'll get an error indicating status of the response.

If you want the errors to be ignored in the report, you can append '$' symbol to the end of the command to use the error supression mode, for example:

// this url gives us a 404 Not Found error, but we can use `httpPost$` to suppress the error
var response = UI.httpPost$("https://google.com/notfound") 

Example

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

// update pet "0"
let res = UI.httpPost("https://petstore.swagger.io/v2/pet", 
    {
        // form data to send
        data: {
            "id": 0,
            "name": "doggie",
            "status": "available"
        },
        // expect response to be json
        responseType: "json", 
    }
)

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