Exposing a site api with http functions query

Hi Wix Coders,

Noting following …

  1. Collection permissions - All set to Anyone except Delete (Admin only)
  2. Confirm able to update Collection using https://support.wix.com/en/article/how-to-import-and-export-collection-data-using-the-wix-data-api
    … i.e. assume Collection permissions are acceptable for external update.

Now trying POST option from https://support.wix.com/en/article/exposing-a-site-api-with-http-functions ;

Backend http-functions.js contains;

export function post_myFunction(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
// get the request body
return request.body.text()
.then( (body) => {
// insert the item in a collection
return wixData.insert(“CntrRetCollection”, JSON.parse(body));
} )
.then( (results) => {
options.body = {
“inserted”: results
};
return created(options);
} )
// something went wrong
. catch ( (error) => {
options.body = {
“error”: error
};
return serverError(options);
} );
}

JSON looks like (i.e. same format as update exercise above) …
[
{
“containerNumber”: “ABCD5566778”,
“returnLocation”: “LVL 3, 111 ABC ST, SYD”
},
{
“containerNumber”: “DEFG6677889”,
“returnLocation”: “LVL 3, 111 ABC ST, SYD”
}
]

… and we’re using Excel 2016 VBA (Microsoft Scripting Runtime enabled in VBA references) to send the JSON string;

Sub SendJson()
Dim objHTTP As Object
Dim Json As String
Json = Range(“A5”)
Dim result As String
Set objHTTP = CreateObject(“MSXML2.XMLHTTP”)
'Set objHTTP = CreateObject(“MSXML2.ServerXMLHTTP”)
Url = " https://oalbps.wixsite.com/cret/_functions/myFunction "
objHTTP.Open “POST”, Url, False
objHTTP.setRequestHeader “Content-type”, “application/json”
objHTTP.Send (Json)
result = objHTTP.responseText
'Some simple debugging
Range(“A25”).Value = result
Range(“A26”).Value = Json
Set objHTTP = Nothing
End Sub

… but getting following error in Excel cell A25;

{“error”:{“code”:“WD_VALIDATION_ERROR”}}

Any clues on cause of this error? Are SSL certificates or API tokens required or is error due to some data format error?

Thanks.

Hi All,

Found root cause of error is the format of the JSON string i.e. Json = “{”“containerNumber”“:”" ABCD5566778 “”,““returnLocation””:“” LVL 3, 111 ABC ST, SYD “”}" works fine. Only issue now is working out how to send multiple records.

Tks/Rgds

II am looking for some code showing how to perform CRUD against a collection named: ‘userData’. Anyone has any idea on how to code the function? Do I need a separate function for each CRUD operation, or could I just create a main one with the different modules needed by the calling app?

Thanks

Michael

Please follow our Forum Guidelines and post your question as a new forum post and not on a old forum post from two years ago.

Have just noticed that you have posted as a new forum post, therefore you don’t need to jump onto this old one.

As posted from Liran (ex-Wix Mod) in a reply from previous Crud post.

You can’t automate the import process, but you can use http-functions to expose an endpoint that will update your database using code.

Then you can use a tool like Zapier to trigger the update when some event occurs.

This means that you need to have an API to read from your CSV file (so you can send the data to the http-function).

Closed