UNPKG

5.36 kBMarkdownView Raw
1# fetch
2
3Fetch url contents. Supports gzipped content for quicker download, redirects (with automatic cookie handling, so no eternal redirect loops), streaming and piping etc.
4
5## Install
6
7 npm install fetch
8
9## Usage
10
11See examples folder for a complete example
12
13## Fetch from URL
14
15`fetch.fetchUrl(url [, options], callback)`
16
17Where
18
19 * **url** is the url to fetch
20 * **options** is an optional options object
21 * **callback** is the callback to run - `callback(error, meta, body)`
22
23Example
24
25 var fetchUrl = require("fetch").fetchUrl;
26
27 // source file is iso-8859-15 but it is converted to utf-8 automatically
28 fetchUrl("http://kreata.ee/iso-8859-15.php", function(error, meta, body){
29 console.log(body.toString());
30 });
31
32**NB** If the file has been marked with charset other than utf-8, it is converted automatically.
33
34By default `iconv-lite` is used for charset conversion. If you want to use `node-iconv` module instead,
35add `"iconv": "*"` to your package.json file, it will be picked up by `fetch` automatically.
36
37## Streaming
38
39`fetch.FetchStream(url [, options]) -> Stream`
40
41Where
42
43 * **url** is the url to fetch
44 * **options** is an optional options object
45
46With events:
47
48 * **data** with a data chunk - `function(chunk){}`
49 * **meta** with some information about the response `function(meta){}`
50 * **end** when the receiving is ready
51 * **error**
52
53Example
54
55 var FetchStream = require("fetch").FetchStream;
56
57 var fetch = new FetchStream("http://google.com");
58
59 fetch.on("data", function(chunk){
60 console.log(chunk);
61 });
62
63## Options
64
65Possible option values
66
67 * **maxRedirects** how many redirects allowed, defaults to 10
68 * **disableRedirects** set to true if redirects are not allowed, defaults to false
69 * **headers** optional header fields, in the form of `{'Header-Field':'value'}`
70 * **maxResponseLength** maximum allowd length for the file, the remainder is cut off. Defaults to `Infinity`
71 * **method** defaults to GET
72 * **payload** request body
73 * **disableGzip** set to false, to disable content gzipping, needed for Node v0.5.9 which has buggy zlib
74 * **cookies** an array of cookie definitions in the form of `['name=val']`
75 * **cookieJar** for sharing cookies between requests, see below
76 * **outputEncoding** valid for `fetchUrl`
77 * **disableDecoding** valid for `fetchUrl`, set to true to disable automatic charset decoding to utf-8
78 * **overrideCharset** valid for `fetchUrl`, set input encoding
79 * **asyncDnsLoookup** use high performance asyncronous DNS resolution based on c-ares instead of a thread pool calling getaddrinfo(3)
80 * **timeout** set a timeout in ms
81 * **agent** pass-through http.request agent parameter
82
83
84## Meta object
85
86Meta object contains following fields:
87
88 * **status** HTTP status code
89 * **responseHeaders** response headers
90 * **finalUrl** last url value, useful with redirects
91 * **redirectCount** how many redirects happened
92 * **cookieJar** CookieJar object for sharing/retrieving cookies
93
94## Headers
95
96Request headers can be set with `options.headers`
97
98 options = {
99 headers:{
100 "X-My-Header": "This is a custom header field"
101 }
102 }
103
104## User-Agent
105User-Agent value can be set with `options.headers['User-Agent']` value. Defaults to `"FetchStream"`
106
107 options = {
108 headers: {
109 "User-Agent": "MyUseragent/1.0"
110 }
111 }
112
113## Cookies
114Cookies can be set with `options.cookies` which takes an array with cookie definitions
115
116 options = {
117 cookie: ["name=value", "key=value; path=/; secure"]
118 }
119
120Paths, domain, expire and other cookie settings are honored, so try not to set cookies with expire dates in the past. If domain is not set, any domain will pass, same for paths.
121
122**NB** Do not set cookie field directly in request header as it will be overwritten.
123
124## Cookie sharing
125
126Cookies can be shared between different requests, this can be achieved with `CookieJar`
127
128 var fetch = require("fetch");
129
130 var cookies = new fetch.CookieJar();
131
132 // add one cookie for testing
133 cookies.setCookie('alfa=beta; path=/;');
134
135 // create a FetchStream with custom CookieJar
136 var f = fetch.FetchStream("http://www.example.com/page1",{cookieJar: cookies});
137
138 f.on("end", function(){
139 // if cookies were set with the previos request, the data is
140 // saved in 'cookieJar' and passed to the next request
141 fetch.FetchStream("http://www.example.com/page1",{cookieJar: cookies});
142 });
143
144
145## Redirects
146
147Redirects are on by default, use `options.disableRedirects` to disable. Maximum redirect count can be set with `options.maxRedirects` (defaults to 10)
148
149 options = {
150 disableRedirects: true
151 }
152
153 options = {
154 maxRedirects: 100
155 }
156
157## Disable Gzip support
158
159Gzip and Deflate support is automatically on. This is problematic in Node v0.5.9 and below since Zlib support on these versions is buggy with unpacking and tends to yield in error.
160
161 options = {
162 disableGzip: true
163 }
164
165## Piping to file
166
167`FetchStream` is a readable Stream object and thus can be piped. For example stream URL contents directly to a file:
168
169 var FetchStream = require("fetch").FetchStream,
170 fs = require("fs"),
171 out;
172
173 out = fs.createWriteStream('file.html');
174 new FetchStream("http://www.example.com/index.php").pipe(out);
175
176## License
177
178BSD
\No newline at end of file