Post http request to my wix site.

Hey there,

I’m new to http requests, html and json all together. I’m trying to send a simple post request to my website to add an item to one of my collections. However, I can’t seem to get it working.
I managed to get a get request working but for some reason the post request just wont work.

Here are some screenshots:

I searched the error output online with barely any results (nothing helpful).
Could anyone help me out with this one? It would be much appreciated :slight_smile:

EDIT: My collection settings are set to anyone for everything except for the “delete content” field.

Cheers!

You might want to explore these two examples which demonstrate how to expose your site API:

Hey @yisrael-wix ! After looking at some documentation I updated my code with the following with no success.

Would you have any insight as to what exactly I am doing wrong?
Btw I am a great fan of craft brew myself :slight_smile:

Thank you!

@raphaelhuotmtl Now that you mention brewing I’m sure you have peaked Yisrael’s (& GOS’s) interest :smiley:

In the meantime, what is your collection name? The code on your initial post shows your collection id as fastLifeLeaderboard but on the second it is myCollection

Try using console.log() to debug your code and since this is a backend file you could use StackDriver to monitor your console logs.

EDIT: Check line 24. Where are you getting results from?

@shantanukumar847
Somebody mention beer :grin::grin:

@shantanukumar847 Thanks for pointing this out! Just fixed it back to fastLifeLeaderboard. The issue still persist however. I’ve been meaning to try out the console log but had no idea how! Thank for the link I’ll see if I can learn more about my issue :slight_smile:

@raphaelhuotmtl
Yes do work with the console log within Wix as Shan has mentioned, it can be your best friend (sometimes!), although some might say that you should now be looking at using debugging tools like Chromes own DevTools instead, however for yourself the console log should be perfectly fine for your needs.

@shantanukumar847 Thanks to you I managed to test out the console and it seems like everything should be working fine:

I’m not too sure what to do from here.
wixData.insert is supposed to be handling raw JSON right? or am I supposed to parse it?

I submitted a callback request to Wix for some help on this and they seemed to have denied it by mail. I feel like I have followed documentation? Is there something I’m missing?

Thanks a lot for your help it is much appreciated :slight_smile:

@raphaelhuotmtl Instead of screenshoting please post your code in a code block so that I can take a look

@shantanukumar847 There you go:

import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
export function post_addUserLeaderboard(request) {
console.log("hey");
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then( (body) => {
console.log(body);
// insert the item in a collection
let leaderboardEntry = {
"title": body.title,
"score": body.score
}
return wixData.insert("fastLifeLeaderboard", leaderboardEntry);
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}

@raphaelhuotmtl Try this:

export function post_addUserLeaderboard(request) {
   let options = {
      "headers": {
      "Content-Type": "application/json"
      }
   };
   return request.body.json()
   .then( (body) => {
      let leaderboardEntry = {
         "title": body.title,
         "score": body.score
      }
      return wixData.insert("fastLifeLeaderboard", leaderboardEntry)
      .then( (result) => {
         options.body = {
            "inserted": result
         };
         return created(options);
      })
      .catch( (error) => {
         options.body = {
             "error": error
         };
         return serverError(options);
      });
    })
    .catch( (error) => {
        options.body = {
           "error": error
             };
        return serverError(options);
     });
}

@shantanukumar847 It returned the same error unfortunately

Is your data being inserted into the database?

@shantanukumar847 No it isn’t. It would be in the live version right?

@raphaelhuotmtl Yes, Is the database ID correct? Check for permissions. Else try this:

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

let auth = {
"suppressAuth": true,
"suppressHooks": true
};

export function post_addUserLeaderboard(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.json()
.then( (body) => {
return database(body, options);
})
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
});
}

function database(body, options) {
let leaderboardEntry = {
"title": body.title,
"score": body.score
};
return wixData.insert("fastLifeLeaderboard", leaderboardEntry, auth)
.then( (result) => {
let item = result;
options.body = {
"status": 200,
"item": item
};
return created(options);
});
}

@shantanukumar847 … it was the wrong id. IT WORKS OMG!! You’re the best! I though editing the name would’ve edited the id but it didn’t. It registers the json in the live version now! Thank you so much for the support and sorry that the issue was from such a simple mistake.