questal
A lightweight javascript library for handling browser-based HTTP requests. For exclusively server-side requests, see Requestal
Install
npm install questal
Include
<!-- The old-fashioned way --> <!-- By Es6 module -->
// Using Webpack/Babel loader or similar // straight into your javascript file;
Basic Usage:
You can make get
, post
, put
, patch
, and delete
requests with Questal
, to make a quick one off request you can capitalize the first letter and call the static version of the method, or you can set more customized options by instantiating a new Questal
instance:
:
//static requestQuestal; //using full Get method instanceconst q = ; let getInstance = q;// do stuffgetInstance;
Pass parameters into the Questal constructor to have them persist through every call made by that instance, then optionally override a parameter on any individual call:
let post = q; //Parameters sent: { url:'/data', data: { id:16, first:'Bill', last: 'Jones' } }post; //Parameters sent: { url:'/params', data: { id:17, first:'Bill', last: 'Nelson' } }post;
Callbacks
//static post requestQuestal;
The data parameter passed to the 'on success' callback is a Questal Response object containing the results of the request.
data:
QuestalResponse defaultType: "text" settings: XMLHttpRequest onreadystatechange: null readyState: 4 timeout: 60000 withCredentials: false upload: XMLHttpRequestUpload … types: 5 "arraybuffer" "blob" "document" "text" "json" code: … data: … html: "…" json: … result: … status: "…" type: "…" url: "…" xml: "…"
data.json:
"names":
Headers
Set or append header properties and they'll automatically be sent after open, for other tasks intended to run on readyState == 1
, use questal.on('ready', callback)
let post = q postheadersaccept = 'json'; //adds 'application/json' to acceptheaders to be setpostheadersencoding = 'multipart'; // sets Content-Type to 'multipart/form-data'postresponsetype = 'json'; //sets response type to application/json
You can check the response object's headers parameter to confirm response headers:
post; //after setup, send requestpost;
Put and Delete
Turn the results of a request into its own file using the put
method
let get = q;get; get;
Then add an event handler to delete the new file
document;
Running npm run test-server
will let you see some of these examples in action locally on localhost:8080
or 127.0.0.1:8080
in your browser.