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.
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
@certified-code
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.
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);
});
}
Collection-Name = “Team”
Code is written down in the Back-End-Section (JSW-File)
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
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
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("Team") //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()
}
}
@russian-dima I think you have the incorrect database id. Note that database name and id can be different. Click on the database collection’s ‘Edit Settings’ to see the id.