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:
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 get(request: wixhttpfunctionrequest-obj): WixHttpFunctionResponse
get Parameters
NAME
TYPE
DESCRIPTION
Returns
Return Type:
Was this helpful?
This example creates a GET HTTP function named myFunction.
1// In http-functions.js23import {ok, notFound} from 'wix-http-functions';45// URL looks like:6// https://www.mysite.com/_functions/myFunction/findMe7// or:8// https://user.wixsite.com/mysite/_functions/myFunction/findMe9export function get_myFunction(request) {10 if(request.path[0] === "findMe") {11 const body = "Found it!";12 return ok({body: body});13 }1415 const body = "Can't find it!";16 return notFound({body: body});17}18
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.
1// In http-functions.js23import {ok, notFound, serverError} from 'wix-http-functions';4import wixData from 'wix-data';56// URL looks like:7// https://www.mysite.com/_functions/myFunction/John/Doe8// or:9// https://user.wixsite.com/mysite/_functions/myFunction/John/Doe10export function get_myFunction(request) {11 let options = {12 "headers": {13 "Content-Type": "application/json"14 }15 };16 // query a collection to find matching items17 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 found23 if(results.items.length > 0) {24 options.body = {25 "items": results.items26 };27 return ok(options);28 }29 // no matching items found30 options.body = {31 "error": `'${request.path[0]} ${request.path[1]}' was not found`32 };33 return notFound(options);34 } )35 // something went wrong36 .catch( (error) => {37 options.body = {38 "error": error39 };40 return serverError(options);41 } );42}43