Add data to database

Hi, I want to basically add information from a api into the wix database.
Below is the the code I used in postman, which allowed me to see the JSON from the api.
How do I receive the request in the wix backend, and add it to the collection?
Thanks.

var data = JSON.stringify({
“categoryKey”: null,
“country”: “USA”,
“distance”: 5,
“location”: “75024”,
“name”: “”
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener(“readystatechange”, function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});

xhr.open(“PUT”, “http://165.227.8.161:8080/radius25/Consumer/AvailableMerchants/”);
xhr.setRequestHeader(“Content-Type”, “application/json”);
xhr.setRequestHeader(“Accept”, “application/json”);
xhr.setRequestHeader(“Access-Control-Allow-Origin”, “*”);
xhr.setRequestHeader(“Cache-Control”, “no-cache”);
xhr.setRequestHeader(“Postman-Token”, “ddbd5608-f2df-4cd4-abb0-02ea71900b14”);

xhr.send(data);

Hi Aashay,

Read the article Accessing 3rd Party Services for more information on accessing external web services. The article Calling Server-side Code from the Front-end with Web Modules might also be useful depending on your use case.

Good luck,

Yisrael

Hi, sorry to ask a question here.

But i tried using " var xhr = new XMLHttpRequest();" in wix code, but I get “XMLHttpRequest is not defined”.

Do I need to import it somewhere?

Thank you.

regards,
Mclaud

XMLHttpRequest is not supported. You should see the articles I referred to in a previous post. Also, see the wix-fetch API .

See the Wix Code Examples Index page for examples on how to access 3rd party services.

Hi @yisrael-wix , thanks for your fast reply. I will try those links. I am a newbie in wix codes and javascript so I am having problems in just saving the html code (from a public webpage) to a string. Would you happen to have a short code for this?

Hi @arcvillagracia !

You can see the examples on wix-fetch - Velo API Reference - Wix.com.

Also there’s Wix Arena https://www.wix.com/arena/info/become-an-expert a place where you can hire an expert for more serious programming tasks.

@arcvillagracia You can also look at Example: MyApi and MyApiClient for details on accessing a 3rd party API or even for creating your own API.