Accessing posted data from kick off labs

I have a landing page with kick off labs. When a form is submitted from that page i used a webhook to my wix website. Kick off labs posts data to my end point url is https://www.mywixsite.com/_funtions/myFunction

This is the top of my backend http-funtion.js

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

export function post_myFunction(request) {
let options = {
“headers” : {
“Content-Type” : “application/json”
}
};

let name;
let email;
if (request.body) {
name = request.body.text(“name”)

if i use request.body.text() i can see all the variables.
I am just not sure what is the syntax to parse it. I need name and email
I have tried alot of different combinations request.body.text(“name”), request.body.text(1), request.body.text[name] to name a few. but i just cant capture the incoming information.

Any help would be appreciated.

Since you are receiving your data as JSON, you probably should be retrieving your data like this:

request.body.json()

You would then access each field according to the normal JSON syntax.

Hi Yisrael,

So i need to have something like this…

const json = ‘{“name”:Peter, "email":peter@peter.com}’ ;
const obj = JSON.parse(json);

then i could access email as
userEmail = obj.email

since i can’t use a static json input…i tried

const json = request.body.json()
const obj = JSON.parse(json);

but this does not work. I get an error.
any suggestions?

The following line returns a JSON object:

const  json = request.body.json()

You don’t need to parse it. This should work:

const  json = request.body.json();
let email = json.email;

Otherwise, you should inspect the contents with:

console.log(json);

Not sure why its not working. But i don’t want to take all your time.

One final question as a work around…

If i have…

const json = request.body.json()

what is the syntax for this…

let jsonquotes
jsonquotes = ‘+json+’

in playing around i noticed that if i put json in a data base it doesn’t have those single quotes.

and f use the static version without quotes it doesn’t work
but if i put the single quotes in it works.

So if i put single quotes around the json varialbe coming.

I could use that as a work around until i figure out the proper way.

if i use this…it works
const json = '{“name”:Peter, “email”:peter@peter.com}’ ;
const obj = JSON.parse(json);
useremail = obj.email

but without quotes doesnt work. which is what comes back from request.body.json()
const json = {“name”:Peter, “email”:peter@peter.com} ;

so as a work around just need to add quotes after. not sure of syntax

Thanks in advance

You are sort of getting confused between a JSON object, and a stringified version of the JSON object.

You said that this woks:

const  json = '{"name":Peter, "email":peter@peter.com}'; 
const obj = JSON.parse(json);
useremail = obj.email;

You are starting with a json string. You then convert it into a JSON object, and then you get the email field.

This will also work:

const  json = {"name":Peter, "email":peter@peter.com}; 
useremail = json.email;

I start with a JSON object (notice, no quotes). I then directly get the email field from the JSON object.

Don’t worry - it’s really not too bad. Take a look at the article Working with JSON.

question… if i put the results from request.body.json(); into a database this is what i get…

{“isFulfilled”:false,“rejectionReason”:{“name”:“JsonSyntaxError”,“errorGroup”:“User”},“isRejected”:true}

I was talking with another person and he mentioned that i may need to install a json library? If thats the case maybe thats why its not working? If so how do i install this?

Also this was a good video https://www.youtube.com/watch?v=tYMq5dfgb3w&t=280s

but it uses the get method. Do you guys have this for the post method?