Class for manipulating HTTP headers.
Usage
Using response headers object (as in http.IncomingMessage, http.ServerResponse, etc.)
const jfHttpHeaders = ;const httpHeaders = messageheaders;console; // application/jsonconsole; // http://localhost:3333
Using raw response headers as string
/** * Server response is:HTTP/1.0 206 Partial ContentContent-Length: 26012Content-Range: bytes 21010-47021/47022Content-Type: image/gifDate: Wed, 15 Feb 2017 06:25:24 GMTLast-Modified: Wed, 15 Feb 2017 04:58:08 GMT */const jfHttpHeaders = ;const httpHeaders = ;httpHeaders;console; // bytes 21010-47021/47022console; // Wed, 15 Feb 2017 06:25:24 GMTconsole; // { code : '206', text : 'Partial Content', version : { major : '1', minor : '0' } }
Building headers and sending request using differents libraries
XMLHttpRequest
must be open before set headers,
http.request
needs headers as option, etc. So, each library use a different approach.
Using jfHttpHeaders
you can split your logic in 2 parts:
- Building body using a lib and setting headers using jfHttpHeaders and return
an instance of
jfHttpHeaders
as configuration ready for another lib. - Sending request using another lib. Before call send, you can set headers iterating over jfHttpHeaders instance using for..of loop.
Using jfHttpHeaders as mediator can simplify your logic.
const jfHttpHeaders = ;const httpHeaders = ;// Preparing body before sending request.body = ...;httpHeaders;httpHeaders;//...//...//...// Now, send the request using an instance of XMLHttpRequest// jfHttpHeaders is iterable, so you can use for..of loop.for let header of httpHeaders xhr;xhr; // Or you can use node too
Or you can extend jfHttpHeaders
and to add other methods for pass an instance of
your class bewteen your libraries.
More examples
You can view in test.js
some examples more about use jfHttpHeaders
.