Autify JavaScript Snippets
Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto mode Back to homepage
Edit page

Send GET / POST request synchronously

Sends a GET / POST request to the specified API endpoint.

Since these codes are samples, please set appropriate values for the header and body according to the specifications of the API to be requested.

XMLHttpRequest is used to send HTTP requests.

Send GET request to a URL

var url = "API URL";
var xhr = new XMLHttpRequest();

xhr.open("GET", url, false);
xhr.send();

if (xhr.status !== 200) {
  throw new Error("Error " + xhr.status);
}

console.log(JSON.parse(xhr.response));

Send POST request to a URL

var url = "API URL";
var xhr = new XMLHttpRequest();

xhr.open("POST", url, false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("key=value");

if (xhr.status !== 200) {
  throw new Error("Error " + xhr.status);
}

console.log(JSON.parse(xhr.response));