Search.../

put( )

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

Description

The HTTP PUT method is usually called by consumers to update an existing resource.

If defined in this way and the resource is updated, the function should respond with a 200 (OK) status code. If defined in this way and the resource is created because it did not exist, you should respond with a 201 (Created) status code.

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 put() 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 PUT 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. When you make changes to production endpoints you must publish your site for them to take effect. Testing endpoints will use the latest code in the editor.

Syntax

function put(request: wixhttpfunctionrequest-obj): WixHttpFunctionResponse

put Parameters

NAME
TYPE
DESCRIPTION
request

The request object.

Returns

Was this helpful?

Create a PUT HTTP function

This example creates a PUT HTTP function named myFunction.

Copy Code
1// In http-functions.js
2
3import {ok} 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 put_myFunction(request) {
10
11 return request.body.text()
12 .then( (body) => {
13
14 // update the info from the body somewhere
15 return ok();
16 } );
17}
18
Create a PUT HTTP function

This example creates a PUT HTTP function named myFunction that updates an item from the request's body in a collection named myCollection. If the item is updated successfully, an OK response is returned.

Copy Code
1// In http-functions.js
2
3import {ok, 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 put_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 // update the item in a collection
20 return wixData.update("myCollection", body);
21 } )
22 .then( (results) => {
23 options.body = {
24 "inserted": results
25 };
26 return ok(options);
27 } )
28 // something went wrong
29 .catch( (error) => {
30 options.body = {
31 "error": error
32 };
33 return serverError(options);
34 } );
35}
36