Post with http functions

I am trying to post json data from an automation tool. I have created a file in wix code in the backend titled http-functions.js with the follow code.

import {created, serverError} from ‘wix-http-functions’;
import wixData from ‘wix-data’;

// URL looks like:
// https://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(“PAData”, body);
} )
.then( (results) => {
options.body = {
“results”: results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
“error”: error
};
return serverError(options);
} );

}

I am trying to write an array to my PAData collection with a Invoke Web Service module in my automation program. The array as follows:

{“PAData”:[

{"title":"test"}, 
{"paOwner":"Test"}, 
{"address": "123test"}, 
{"parcelId": "1235"}, 
{"paLegal": Lot 21 Test"}, 

]}

I would appreciate any help. I have been struggling with this for a week and can not figure out what is going wrong. I believe I am reaching the server, but getting error 500 code returned. Not sure if I should be authenticating or what. I am not finding detailed information on how to post data directly to a collection not using a form. Thanks all.

1 Like

Check this post , see if it helps

Yes I found your post a few days ago and have it working. However, will setting permissions to anyone put my collection at risk for someone to access. Should be able to authenticate. Thanks for the response.

Hi Eddie,

Do you still need help with compute functions?

I was able to make the Post function work thanks to Michelle post above, however I am now going to work on the Get function. My problem is that I have I collection that is read/write for site author only, but with http functions permissions must be set to anyone this will break my table displaying the users data only. What I need is to be able to Get/Post/Update by admin on site author data in a collection. Unless there is another option I am not seeing. Thank you for your response.