Open Request JS 📣
Simply send http requests.
Install
npm install open-request-js
Usage
var Request = ; // To be used from node.var Request = ; // To be used from browser. var request = '/users/:id?lang=en'; requesturlpathid = 4;requesturlparameterslang = 'fr';requesturlstring;// => "/users/4?lang=fr" request;
Why does the require statement depend on wether it's meant to be use from node or from browser?
Because from node, Request
needs to require XMLHttpRequest while it already exists in the browser.
By the way, you will not be able to use html
and xml
HttpResponse
accessors from node neither because (at the moment) they depend on browser-exclusive APIs.
Request
Kind: global class
- Request
- new Request([url], [method], [data])
- instance
- .method :
string
- .actualMethod :
string
- .url :
Url
|string
- .data :
Parameters
|Object
|string
- .send(data) ⇒
Promise
- .method :
- static
- .send(url, [method], [data]) ⇒
Promise
- .get(url, [data]) ⇒
Promise
- .post(url, [data]) ⇒
Promise
- .put(url, [data]) ⇒
Promise
- .patch(url, [data]) ⇒
Promise
- .delete(url, [data]) ⇒
Promise
- .send(url, [method], [data]) ⇒
new Request([url], [method], [data])
Creates a Request object.
Param | Type | Default | Description |
---|---|---|---|
[url] | string | Url |
The url of the request. | |
[method] | string |
"get" |
The request method. |
[data] | Object | Parameters |
The data to send. |
string
request.method : The method of the request. When set it will be checked for being included in 'get', 'post', 'put', 'patch' and 'delete'. If not it will be replaced by 'get'.
Kind: instance property of Request
string
request.actualMethod : The actual method that will be used to send the request (wether 'get' or 'post').
Kind: instance property of Request
Read only: true
Url
| string
request.url : The request url.
Kind: instance property of Request
Parameters
| Object
| string
request.data : The request data. When set it will be parsed to a Parameters object.
Kind: instance property of Request
Promise
request.send(data) ⇒ Sends the request, merging the given data with the instance data to a new object.
Kind: instance method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
data | Parameters | Object | string |
The data to send, in addition to the instance data. |
Promise
Request.send(url, [method], [data]) ⇒ Sends a request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Default | Description |
---|---|---|---|
url | string | Url |
The url of the request. | |
[method] | string |
"get" |
The request method. |
[data] | Object | Parameters |
The data to send. |
Promise
Request.get(url, [data]) ⇒ Sends a get request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
url | string | Url |
The url of the request. |
[data] | Object | Parameters |
The data to send. |
Promise
Request.post(url, [data]) ⇒ Sends a post request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
url | string | Url |
The url of the request. |
[data] | Object | Parameters |
The data to send. |
Promise
Request.put(url, [data]) ⇒ Sends put get request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
url | string | Url |
The url of the request. |
[data] | Object | Parameters |
The data to send. |
Promise
Request.patch(url, [data]) ⇒ Sends patch get request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
url | string | Url |
The url of the request. |
[data] | Object | Parameters |
The data to send. |
Promise
Request.delete(url, [data]) ⇒ Sends a delete request.
Kind: static method of Request
Returns: Promise
- A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.
Param | Type | Description |
---|---|---|
url | string | Url |
The url of the request. |
[data] | Object | Parameters | Parameters |
The data to send. |
HttpResponse
Kind: global class
new HttpResponse([data], [status])
Creates a HttpResponse object.
Param | Type | Description |
---|---|---|
[data] | string |
The data of the response. |
[status] | Status | Object |
The status of the response. |
Status
| Object
httpResponse.status : The response status. When set, it will be parsed to a Status object
Kind: instance property of HttpResponse
string
httpResponse.text : The response in text format.
Kind: instance property of HttpResponse
Object
httpResponse.json : The response in json format.
Kind: instance property of HttpResponse
Element
httpResponse.xml : The response in XML format.
Kind: instance property of HttpResponse
Element
httpResponse.html : The response in HTML format.
Kind: instance property of HttpResponse
Status
Kind: global class
- Status
- new Status([code], [text])
- .code :
number
|string
- .text :
string
- .info :
boolean
- .success :
boolean
- .redirection :
boolean
- .error :
boolean
- .set([code], [text])
new Status([code], [text])
Creates a Status object.
Param | Type | Description |
---|---|---|
[code] | number | string |
The code of the response. |
[text] | string |
The text of the response. |
number
| string
status.code : The code of the response. When set, it will be parsed to an integer, and set to 0 if it's invalid.
Kind: instance property of Status
string
status.text : The text of the response.
Kind: instance property of Status
boolean
status.info : Tells if this is an info.
Kind: instance property of Status
boolean
status.success : Tells if this is a success.
Kind: instance property of Status
boolean
status.redirection : Tells if this is a redirection.
Kind: instance property of Status
boolean
status.error : Tells if this is an error.
Kind: instance property of Status
status.set([code], [text])
Set the status
Kind: instance method of Status
Param | Type | Description |
---|---|---|
[code] | number | string |
The code of the response. |
[text] | string |
The text of the response. |