Edit Page

io.socket.request()

Send a virtual request to a Sails server using Socket.IO.

This function is very similar to io.socket.get(), io.socket.post(), etc. except that it provides lower-level access to the request headers, parameters, method, and URL of the request.

Using the automatically-created io.socket instance:

io.socket.request(options, function (resData, jwres)){
 // ...
 // jwres.headers
 // jwres.statusCode
 // jwres.body === resData
 // ...
});

Usage

Option Type Details
method The HTTP request method; e.g. 'GET'.
url The destination URL path; e.g. "/checkout".
data Optional. If provided, this request data will be JSON-encoded and included as the virtual HTTP body.
headers Optional. If provided, this dictionary of string headers will be sent as virtual request headers.
Callback
Argument Type Details
1 resData Data received in the response from the Sails server (=== jwres.body, and also equivalent to the HTTP response body).
2 jwres A JSON WebSocket Response object. Has headers, a body, and a statusCode.

Example

io.socket.request({
 method: 'get',
 url: '/user/3/friends',
 data: {
 limit: 15
 },
 headers: {
 'x-csrf-token': 'ji4brixbiub3'
 }
}, function (resData, jwres) {
 if (jwres.error) {
 console.log(jwres.statusCode); // => e.g. 403
 return;
 }
 console.log(jwres.statusCode); // => e.g. 200
});

Notes

  • A helpful analogy might be to think of the difference between io.socket.get and this method as the difference between JQuery's $.get and $.ajax.
  • Remember that you can communicate with any of your routes using socket requests.
  • Need to set custom headers for all outgoing requests? Check out io.sails.headers.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Check out the official Sails VS Code extension

Documentation

Reference

Sails logo

Reference

AltStyle によって変換されたページ (->オリジナル) /