Error using http-function post()

Hello.
I’m trying to populate a Wix collection using Wix http-function post() method.
I’ve already created an empty collection with field names matching the json I want to post.

I’ve created a backend file called http-function.js with the following code:

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

export function post_addProperty(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("Properties", body);
  	} )
	  .then( (results) => {
    	options.body = {
    		"inserted": results
			};
    	return created(options);
	  } )
		// something went wrong
	  .catch( (error) => {
			options.body = {
				"error": error
			};
	    return serverError(options);
	  } );
}

I’m using a Java Jersey web-service to call the Wix API.
At the moment, however, I’m trying to get it working with Chrome Postman first.
When I send the request I get a 500 internal serve error:
{“error”:{“code”:“WD_PERMISSION_DENIED”}}

I searched the Wix forum and also googled it but I’ve found nothing.
What I am missing here? Can anyone give me any hint on how to proceed?

1 Like

Ok, I found the solution.
I had to edit collection permission and select ‘Anyone’ where is asked who can create content for this collection. Pretty simple, but maybe a note here wix-http-functions - Velo API Reference - Wix.com could be useful. I wasted few hours for that.

For anyone interested, I also added more code to the previous snippet:

// get the request body
	return request.body.json()
		.then( (body) => {
			// insert the item in a collection
			let recordInsert = {
				"propertyId": body.propertyID,
				"property_marker_title": body.title,
				"property_marker_snippet": body.snippet,
				//etc.etc.
			}
    	return wixData.insert("Properties", recordInsert);
  	} )

It worked both from Chrome Postman and from my Java Jersey web-service.

You can find a good video that uses Google Sheets as data source for Wix (but helped me to solve my actual issue) here: Use Google Sheet as data source for Wix Code - YouTube

Can something like this be used to post to Google Sheets on a Form Submission in Wix ?

I wonder if anyone manage to get this to work. I am looking for ways to insert the data into my sandbox collections. Is there a way to do this? or the only way to use wix-functions is to reference the live collection only.

I also noticed when inserting into live collection - it created a new columns with a square bracket. Strange…Is this a bug? http://prntscr.com/jgeg71
Here is what I sent to the function: http://prntscr.com/jgegld
Here is the function: http://prntscr.com/jgegwq

Is there a way to create a record where the field is without the square bracket?

You’re getting the square bracket because the Data Collection property name is not being recognized for the field; the square bracket is a new field that’s been added. Somewhere in your function and in the tooling setup sending to your http-function, there’s a name mismatch. The field name in your Data Collection is different from the name in your .js file.