import {PostRequest} from '@serenity-js/rest/lib/model'
public class | source

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:

Question<Promise<AxiosRequestConfig>>HTTPRequest → PostRequest

Examples:

Add new resource to a collection
 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')),
 );
Submit a HTML form
 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

Overrides the default Axios request configuration provided when CallAnApi Ability was instantiated.

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>

Resolves the Question in the context of a given Actor

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:

NameTypeAttributeDescription
resourceUri Answerable<string>

The URI where the Actor should send the HTTPRequest.

Returns:

PostRequest

Public Methods

public using(config: Answerable<AxiosRequestConfig>): PostRequest source

Overrides the default Axios request configuration provided when CallAnApi Ability was instantiated.

Params:

NameTypeAttributeDescription
config Answerable<AxiosRequestConfig>

Axios request configuration overrides

Returns:

PostRequest

public with(data: Answerable<any>): PostRequest source

Configures the object with a request body.

Params:

NameTypeAttributeDescription
data Answerable<any>

Data to be sent to the resourceUri

Returns:

PostRequest