John Kwong's Tech Notes Sharing my thoughts about tech and art.

Testing API Requests With XHR

It is easy to test api service with XHR. Just open Chrome Console and enter XHR syntax to test it. Sending HTTP GET request

var request = new XMLHttpRequest();
var url = 'https://abc.com/api/v1/product/' + id ;
request.open('GET', url, true);
request.send();

Sending HTTP POST request

POST Method
var request = new XMLHttpRequest();
var url = 'https://abc.com/api/v1/product/' ;
request.open("POST", url);
request.send();

Sending HTTP GET request with Header

var request = new XMLHttpRequest();
var url = 'https://abc.com/api/v1/product/' + id ;
request.open('GET', url, true);
request.setRequestHeader("Authorization", "Bearer " + access_token);
request.send();

Sending HTTP POST request with Header

var request = new XMLHttpRequest();
var url = 'https://abc.com/api/v1/product/' ;
request.open("POST", url);
request.setRequestHeader("Content-Type", "application/json");
request.send();