I have created a custom rest API and placed it in my http-functions.js. I can all the API fine, but I get the following response error back. {“error”:{“name”:“Error”,“errorGroup”:“User”,“code”:“WD_VALIDATION_ERROR”}}
Is there something that needs to be included in the headers of the POST in addition to the payload? If so, I can’t find any docs on it.
You may be using an invalid token.
Seeing that there’s no solution yet, I’d just like to share my findings as I’ve recently ran into this issue and had it resolved.
The result is likely the response when you are trying to update multiple records at once, however the default code example provided by Wix only works for 1 record:
Wix Example: This works if you have only 1 record, because that’s what the wixData.insert() or wixData.update() does.
import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
// URL looks like:
// https://www.mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function post_myFunction(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then( (body) => {
// insert the item in a collection
return wixData.insert("myCollection", body);
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
If you have multiple records to update at once, simply replace wixData.insert() or wixData.update() with wixData.bulkInsert() or wixData.bulkUpdate() instead:
import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
// URL looks like:
// https://www.mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/
export function post_myFunction(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then( (body) => {
// insert the item in a collection
return wixData.bulkInsert("myCollection", body);
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}