PostRequest
The HTTP POST method requests that the origin server accepts
the entity enclosed in the request as a new subordinate of the resource
identified by the resourceUri
.
This means that the POST should be used when you want to create a child resource under a collection of resources.
POST request is neither safe, nor idempotent. This means that if you retry a POST request N times, a correctly implemented HTTP REST API will create N resources with N different URIs.
Extends:
Examples:
import { Actor } from '@serenity-js/core';
import { CallAnApi, LastResponse, PostRequest, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';
const actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com/api'));
actor.attemptsTo(
Send.a(PostRequest.to('/books').with({
isbn: '0-688-00230-7',
title: 'Zen and the Art of Motorcycle Maintenance: An Inquiry into Values',
author: 'Robert M. Pirsig',
})),
Ensure.that(LastResponse.status(), equals(201)),
Ensure.that(LastResponse.header('Location'), equals('/books/0-688-00230-7')),
);
import { Actor } from '@serenity-js/core';
import { CallAnApi, LastResponse, PostRequest, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';
import { stringify } from 'querystring';
const
actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com')),
formData = stringify({
name: actor.name,
email: `${ actor.name }@example.com`,
text: 'Your website is great! Learnt a lot :-)'
});
actor.attemptsTo(
Send.a(PostRequest.to('/feedback').with(postData).using({
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': formData.length
}
})),
Ensure.that(LastResponse.status(), equals(200)),
Ensure.that(LastResponse.header('Location'), equals('/feedback/thank-you.html')),
);
Tests:
Static Method Summary
Static Public Methods | ||
public static |
to(resourceUri: Answerable<string>): PostRequest Configures the object with a destination URI. |
Method Summary
Public Methods | ||
public |
using(config: Answerable<AxiosRequestConfig>): PostRequest |
|
public |
with(data: Answerable<any>): PostRequest Configures the object with a request body. |
Inherited Summary
From class HTTPRequest | ||
public |
subject: string |
|
public |
answeredBy(actor: AnswersQuestions & UsesAbilities): Promise<AxiosRequestConfig> |
Static Public Methods
public static to(resourceUri: Answerable<string>): PostRequest source
Configures the object with a destination URI.
When the resourceUri
is not a fully qualified URL but a path, such as /products/2
,
it gets concatenated with the URL provided to the Axios instance
when the CallAnApi Ability was instantiated.
Params:
Name | Type | Attribute | Description |
resourceUri | Answerable<string> | The URI where the Actor should send the HTTPRequest. |
Public Methods
public using(config: Answerable<AxiosRequestConfig>): PostRequest source
Params:
Name | Type | Attribute | Description |
config | Answerable<AxiosRequestConfig> | Axios request configuration overrides |
public with(data: Answerable<any>): PostRequest source
Configures the object with a request body.
Params:
Name | Type | Attribute | Description |
data | Answerable<any> | Data to be sent to the |