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:
Copy Codehttps://www.{user_domain}/_functions/<functionName>
Free sites:
Copy Codehttps://{user_name}.wixsite.com/{site_name}/_functions/<functionName>
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
Returns
Return Type:
Was this helpful?
This example creates a POST HTTP function named myFunction.
1// In http-functions.js23import {created} from 'wix-http-functions';45// 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) {1011 return request.body.text()12 .then( (body) => {1314 // insert the info from the body somewhere1516 return created();17 } );18}19
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.
1// In http-functions.js23import {created, serverError} from 'wix-http-functions';4import wixData from 'wix-data';56// 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 body17 return request.body.json()18 .then( (body) => {19 // insert the item in a collection20 return wixData.insert("myCollection", body);21 } )22 .then( (results) => {23 options.body = {24 "inserted": results25 };26 return created(options);27 } )28 // something went wrong29 .catch( (error) => {30 options.body = {31 "error": error32 };33 return serverError(options);34 } );35}36