https-service-legacy
v1.0.6
Published
A wrapper around the node https request method.
Readme
https-service
A wrapper around the node https request method.
Version 1.0.6
1. Installation
$ npm install --save https-service-legacy2. Usage
var HttpsService = require('https-service-legacy');
var service = new HttpsService('httpbin.org');
service.get('/get', (err, body) => {
console.log(body);
}3. API
The package exports the HttpsService class. This class provides the request method and various convenience methods that call the request method. The request method can be overridden in a subclass. One reason, for example, is adding header values. Be sure to call super.request(...) when overriding this method.
3.1 constructor
HttpsService(hostname)Creates a new HttpsService instance for the specified host. The hostname can also be a complete URI if the port is not the default HTTPS port number (443). In that case, the scheme must be specified and it must be https.
Examples:
var service = new HttpsService('example.com');var service = new HttpsService('https://example.com:9000');3.2 request
service.request(method, path, headers, data, callback)Sends an HTTPS request to the host specified in the constructor.
- The
methodshould be one ofGET,HEAD,OPTIONS,TRACE,POST,PUT,PATCHorDELETE. The method is converted to upper case. - The
pathidentifies the resource with respect to the host specified in the constructor. - The
headersmust be an object ornull. - The
dataspecifies the message body and can be aBuffer, a string, or an object. If thedataparameter is an object, then it is processed as follows:- If the
Content-Typeheader isapplication/json, then the data is serialized by callingJSON.stringify. - If the
Content-Typeheader isapplication/x-www-form-urlencoded, then the data is serialized by callingquerystring.stringify. - If the
Content-Typeheader is not set, then the data is serialized by callingJSON.stringifyand theContent-Typeheader is set toapplication/json.
- If the
- The
callbackis called upon completion of the request. On error, it is called ascallback(err). On success, it is called ascallback(null, data, type, headers).- The
datais the response message body. It will be a string if thetypebegins withtextor ends with+xml. It will be an object if thetypeisapplication/json. Otherwise, thedatawill be aBuffer. - The
typeis the value of theContent-Typeheader with all parameters removed. For example, if theContent-Typeistext/html; charset=utf-8, then thetypeargument in the callback will simply betext/html. - The
headersare the full, unmodified headers from the response.
- The
3.3 get, head
service.get(path, [query,] callback)
service.head(path, [query,] callback)Convenience methods for GET and HEAD requests. If the optional query object is specified, it is serialized and appended to the path preceded by a ?. If the path already contains a query, then it is appended with a &.
3.4 post, put, patch
service.post(path, data, callback)
service.put(path, data, callback)
service.patch(path, data, callback)Convenience methods for the POST, PUT, and PATCH requests.
3.5 delete
service.delete(path, callback)Convenience method for the DELETE request.
3.6 Media Types
There are two static media type constants on the HttpsService class:
HttpsService.JSON_MEDIA_TYPE = 'application/json';
HttpsService.FORM_MEDIA_TYPE = 'application/x-www-form-urlencoded';4. License
The MIT License (MIT)
Copyright (c) 2016 Frank Hellwig
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
