Hello Velo-Community,
i am on my way to discover the http-functions and how to use them (never really used them before).
I want to crete an http-endpoint, which will allow users to get data by using an http-request.
Because it is my first attempt to generate it, i have some diffculties to get it to work.
This is what i have right now....
import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
export function get_myFunction(request) {
let options = {
"headers": {"Content-Type": "application/json"}
};
return wixData.query("Example-DB1")
.find()
.then((results) => {
if(results.items.length > 0) {
options.body = {"items": results.items};
return ok(options);
}
});
}
Not sure if everything is coded the right way 😅🙄
How i can test my generated code, if it is working or not?
Start with a simple query and move up from there
import {response} from 'wix-http-functions'; import wixData from 'wix-data'; // You need to suppress authorization if your database has permissions let full_suppress = { "suppressAuth": true, "suppressHooks": true }; export function get_filter(request) { // First check if a query parameter is present in the request or not if(request.query) { return wixData.query("contact01") //remember to put a return here .find(full_suppress) //suppress authorization checks .then( (res) => { // Now check the length of the items retrieved if(res.items.length > 0) { // Items found so return a 200 HTTP response let options = { status: 200, body: res.items }; return response(options); // similar to ok() } else { // No items found, return a 404 HTTP response let options = { status: 404, body: 'No items found' }; return response(options); // similar to notFound() } }) .catch( (error) => { // This will catch any error with the wixdata query let options = { status: 500, body: error }; return response(options); // similar to serverError() }); } else { // Send a 400 response if no query params are found let options = { status: 400, body: 'No query parameters found' }; return response(options); // similar to badRequest() } }
Read my comments, this is a basic scenario on how to create a http function and get database items using an api call
OK, found an good example from Yisrael here....
also tried it out. It worked inside the example when i was requesting for example like... https://mt2-king.wixsite.com/mysite/_functions/recipe ,
but when i am doing the same thing on my own example, it doesn't work at all.
I have right now copy and paste one of the given functions in the given example and modified it a little bit to my own needs.
import {ok, created, badRequest, notFound, serverError} from 'wix-http-functions'; export function get_xxx(request) { let options = { "headers": { "Content-Type": "application/json" } }; let query = wixData.query("Team"); if (request.query) { var query_keys = Object.keys(request.query); query_keys.forEach(function (entry) { query = query.contains(entry, request.query[entry]); }); } return query .find() .then((results) => { //matching items were found if (results.items.length > 0) { options.body = { "items": results.items }; return ok(options); } // no matching items found options.body = { "error": `'${request.path[0]} ${request.path[1]}' was not found` }; return notFound(options); }) // something went wrong .catch((error) => { options.body = { "error": error }; return serverError(options); }); }
1) Collection-Name = "Team"
2) Code is written down in the Back-End-Section (JSW-File)
3) Function called ---> "xxx" (get_xxx)
Why it is not working when i try to call this http-function with....
https://www.media-junkie. com/http-test/_functions/xxx
?????????????????????????????????????????????????????????
@Certified Code - Velo Certified
Need your help a little bit on this topic. Like i could see that you are online, right now. Would be great if you could give some hints on this. 🙄
Thnks so much! great
Ok, still no answer found on this topic.
To understand all this http-process better, i try to simplify it a little bit more....
Back-End-Code-Section:
import {ok} from 'wix-http-functions'; export function get_myFunction(request) { let options = { "headers": {"Content-Type": "application/json"}, "body": "some text here" }; return ok(options); }}
Isn't this all i need to create an http-endpoint?
My wished result is just the little STRING ---> "some text here", which should be shown like the result of this example-link.....
https://api.exchangeratesapi.io/latest
When openening the related http-site directly in the browser, like this example-one:
https://user.wixsite.com/mysite/_functions/myFunction