Releases: sindresorhus/got
5.0.0
Retries
We added retries on network failures with noisy exponential backoff. By default got
will try five times, before returning error.
Retries only applies to network errors (because network is not reliable). Requests with server errors, like 50x
status codes, will not be retried.
You can disable them by passing 0
to retries
option.
Update
$ npm install --save got@5
Highlights
- 0109725 Reject promise on internal exception
- df26918 Catch callbacks in stream mode
- f9f2807 Reduce size for browserify
- 066e612 Remove
response
event onerror
(in stream mode) (#110) - 62ff082 Catch
auth
option passed inurl
string (#106) - 6c59ce8 Add retries on network errors (#98)
- 9ffbbdf Deferred
response
event - 013a2b5
Changes
4.2.0
4.1.1
4.1.0 - x-www-form-urlencoded
- 7bea9c4 If
body
is a plain Object, it will be stringified and sent asapplication/x-www-form-urlencoded
.
4.0.0
Major release with lots of new stuff! Enjoy!
Promise API
got
without callback now returns a promise instead of a stream. The stream API now lives under stream
property (look at the end of section).
got('todomvc.com')
.then(function (res) {
console.log(res.body);
})
.catch(function (err) {
console.error(err.response.body);
});
In resolve
handler you receive a http.response
object with body
as additional property.
In reject
handler we provide an Error object. Unless Error type is RequestError
, it will have response
property with body
property.
This API will be a huge advantage with co
and Generators:
var co = require('co');
co(function * () {
try {
var response = yield got('todomvc.com');
console.log(response.body);
} catch (err) {
console.error(err);
}
});
You can go even further with destructuring and await
from ES7:
async function () {
try {
var {body} = await got('todomvc.com');
console.log(body);
} catch (err) {
console.error(err);
}
}
Stream support is preserved, but now under the streams
property:
got.stream('todomvc.com').pipe(process.stdout);
New Errors
Before 4.0.0
got had one Error class (GotError
) with nested stack traces and custom error messages. This was a nice concept, but in production use some edge cases popped out:
- Error stacks are too long.
- Relevant information often were in one or two levels deep (eg.
error.nested.nested
). - Errors message are hard to parse for
url
.
So we decided to review Errors in new version. In 4.0.0
got now have multiple Error classes for different situations: RequestError
, ReadError
, ParseError
, HTTPError
and MaxRedirectsError
.
In addition to message
and code
properties from the original error, the new errors types also provide additional properties (host
, hostname
, path
and method
) for logging and debugging.
Infinity-agent removed
Custom agent was removed. This returns the ability to configure or replace global Agent, but also returns low maxSockets
value in NodeJS 0.10.x. To fix this you can add this lines at the top of your app (not in reusable modules!):
require('http').globalAgent.maxSockets = Infinity;
require('https').globalAgent.maxSockets = Infinity;
Changes
3.3.1
3.3.0
3.2.0
json
option now sets Accept
header to application/json
(if it was not specified in headers
)
3.1.0
error
event now have additional body
and response
arguments in callback:
got('http://giggle.com')
.on('error', function (error, body, response) {
console.log(error, body, response);
});
3.0.0
Changes
52490b2 New redirect
event
This event fired before any redirect with response
and nextOptions
objects. You can access cookies and store them to be passed with next request in nextOptions
.
got('cookie.com')
.on('redirect', function (res, nextOptions) {
nextOptions.headers.cookie = cookie(res);
});
Breaking changes
2302a1e Redirects enabled only for GET and HEAD methods
As rfc2616 HTTP 1.1 says:
If the 302 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
Now got
will not auto-redirect you with unsafe methods (like POST or DELETE), which can cause lots of troubles.