Simple example:
Import request from ' superagent '; // citation Declarations Request.post (API) . Withcredentials ()// cross-domain . End (Err, res) = { if (Res.ok) { = json.parse (res.text); Else { console.log (' get failed '); } });
1. Get mode
When using get a request to pass a query string, using the .query() method, passing an object, the following code will produce a /search?query=Manny&range=1..5&order=desc request:
request . Get ('/search ') ' Manny ' ) ' (1..5 ' } ) ' desc ' } . End (function(res) { });
or pass a single Large object:
request . Get ('/search ') ' Manny ', Range: ' 1..5 ', Order: ' desc ' } . End ( function(res) { });
.query()Method also allows strings to be passed:
request . Get ('/querystring ') . Query (' search=manny&range=1..5 ') . End ( function(res) { });
or string concatenation:
request . Get ('/querystring ') . Query (' Search=manny ') . Query (' Range=1..5 ') . End (function(res) { });
2. Post request
A typical JSON POST request looks like this, setting an appropriate Content-type header field and then writing some data, in this case just a JSON string:
Request.post ('/user ') . Set (' Content-type ', ' Application/json ') . Send (' {"name": "TJ", " Pet ":" Tobi "}") . End (callback)
Because JSON is very generic, as the default Content-type , the following example is the same as above:
Request.post ('/user ') ' TJ ', pet: ' Tobi ' }) . End (callback)
or call multiple .send() methods:
Request.post ('/user ') ' TJ ' }) ' Tobi ') . End (callback)
The default send string, set Content-type to application/x-www-form-urlencoded , multiple calls will be & connected by, here The result is name=tj&pet=tobi :
Request.post ('/user '). Send (' NAME=TJ '). Send(' Pet=tobi ') . End (callback);
Superagent request data format can be extended, but the default support form and json two formats, want to send data to the type, you application/x-www-form-urlencoded can simply call the method to .type() pass the form parameters on the line, here by default json , The following request will post the name=tj&pet=tobi content:
Request.post ('/user ') . Type (' form ') ' TJ '} ') ' Tobi ' } . End (callback)
3. Set Content-type
The common scenario is to use the .set() method:
Request.post ('/user ') . Set (' Content-type ', ' Application/json ')
An easy way to do this is to call the .type() method, passing in a canonical MIME name, including type/subtype , or a simple suffix like xml , json so, png for example:
Request.post ('/user ') . Type (' Application/json ') request.post ('/user ') . Type ( ' json ')request.post ('/user ') . Type (' png ')
4. Set the acceptance type
As with the .type() simple method, it is also possible .accept() to invoke a method to set the accepted type, which will be request.types referenced to support the passing of a canonical MIME name, including type/subtype , or a simple suffix, like xml json , pngThis, for example:
Request.get ('/user ') . Accept (' Application/json ') request.get ('/user ') . Accept ( ' json ')request.get ('/user ') . Accept (' PNG ')
5. Cross-domain
.withCredentials()Method can activate the ability to send the original cookie, but only if Access-Control-Allow-Origin it is not a wildcard character (*) and Access-Control-Allow-Credentials is ' true '.
request . Get (' http://localhost:4001/') . Withcredentials () . End ( function(res) { assert (= = res.status); Assert (' tobi ' = = res.text); Next (); })
Superagent (Ajax API) that can be used for Nodejs