Search.../

post( )

A function that responds to requests made with the HTTP POST method.

Description

The HTTP POST method is usually called by consumers to create a new resource. If defined in this way and the resource is created, the function should respond with a 201 (Created) status code and usually a reference to the new resource.

Respond to the request by returning a WixHttpFunctionResponse object you create using one of the response(), ok(), created(), notFound(), serverError(), badRequest(), or forbidden() functions.

The post() function is not a function that you call from your code. You define the function in a file named http-functions.js in your site's Backend section. The function is called when your users make HTTP requests using the associated URLs as described below.

All POST requests with the following URL will be routed to this function:

Premium sites:

https://www.{user_domain}/_functions/<functionName>
Copy Code

Free sites:

https://{user_name}.wixsite.com/{site_name}/_functions/<functionName>
Copy Code

Note: You must publish your site at least once before using both the testing and

production endpoints. After that, you save your site for changes you make to testing endpoints to take effect and you publish your site for changes you make to the production endpoints to take effect.

Syntax

function post(request: wixhttpfunctionrequest-obj): WixHttpFunctionResponse

post Parameters

NAME
TYPE
DESCRIPTION
request

wixhttpfunctionrequest-obj

The request object.

Returns

Was this helpful?

Create a POST HTTP function

This example creates a POST HTTP function named myFunction.

Code Example

Copy Code
1// In http-functions.js
2
3import {created} from 'wix-http-functions';
4
5// URL looks like:
6// https://www.mysite.com/_functions/myFunction/
7// or:
8// https://user.wixsite.com/mysite/_functions/myFunction/
9export function post_myFunction(request) {
10
11 return request.body.text()
12 .then( (body) => {
13
14 // insert the info from the body somewhere
15
16 return created();
17 } );
18}
19
Create a POST HTTP function

This example creates a POST HTTP function named myFunction that inserts an item from the request's body into a collection named myCollection. If the item is inserted successfully, a Created response is returned.

Code Example

Copy Code
1// In http-functions.js
2
3import {created, serverError} from 'wix-http-functions';
4import wixData from 'wix-data';
5
6// URL looks like:
7// https://www.mysite.com/_functions/myFunction/
8// or:
9// https://user.wixsite.com/mysite/_functions/myFunction/
10export function post_myFunction(request) {
11 let options = {
12 "headers": {
13 "Content-Type": "application/json"
14 }
15 };
16 // get the request body
17 return request.body.json()
18 .then( (body) => {
19 // insert the item in a collection
20 return wixData.insert("myCollection", body);
21 } )
22 .then( (results) => {
23 options.body = {
24 "inserted": results
25 };
26 return created(options);
27 } )
28 // something went wrong
29 .catch( (error) => {
30 options.body = {
31 "error": error
32 };
33 return serverError(options);
34 } );
35}
36