Search.../

get( )

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

Description

The HTTP GET method is usually called by consumers to retrieve a resource and should have no other effect. If used in this way and the resource is found, the function should respond with a 200 (OK) status code and the requested 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 get() 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 GET 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 get(request: wixhttpfunctionrequest-obj): WixHttpFunctionResponse

get Parameters

NAME
TYPE
DESCRIPTION
request

The request object.

Returns

Was this helpful?

Create a GET HTTP function

This example creates a GET HTTP function named myFunction.

Copy Code
1// In http-functions.js
2
3import {ok, notFound} from 'wix-http-functions';
4
5// URL looks like:
6// https://www.mysite.com/_functions/myFunction/findMe
7// or:
8// https://user.wixsite.com/mysite/_functions/myFunction/findMe
9export function get_myFunction(request) {
10 if(request.path[0] === "findMe") {
11 const body = "Found it!";
12 return ok({body: body});
13 }
14
15 const body = "Can't find it!";
16 return notFound({body: body});
17}
18
Create a GET HTTP function

This example creates a GET HTTP function named myFunction that queries a collection named myCollection to find items based on the path of the request. If matching items are found, an OK response is returned. If no matches are found, a Not Found response is returned.

Copy Code
1// In http-functions.js
2
3import {ok, notFound, serverError} from 'wix-http-functions';
4import wixData from 'wix-data';
5
6// URL looks like:
7// https://www.mysite.com/_functions/myFunction/John/Doe
8// or:
9// https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
10export function get_myFunction(request) {
11 let options = {
12 "headers": {
13 "Content-Type": "application/json"
14 }
15 };
16 // query a collection to find matching items
17 return wixData.query("myUsersCollection")
18 .eq("firstName", request.path[0])
19 .eq("lastName", request.path[1])
20 .find()
21 .then( (results) => {
22 // matching items were found
23 if(results.items.length > 0) {
24 options.body = {
25 "items": results.items
26 };
27 return ok(options);
28 }
29 // no matching items found
30 options.body = {
31 "error": `'${request.path[0]} ${request.path[1]}' was not found`
32 };
33 return notFound(options);
34 } )
35 // something went wrong
36 .catch( (error) => {
37 options.body = {
38 "error": error
39 };
40 return serverError(options);
41 } );
42}
43