用于Node和浏览器的简单的HTTP请求:Popsicle
jopen
10年前
Popsicle设计用于以一种最简单的方法来实现HTTP请求,提供一个一致和直观的API,支持Node和浏览器。
request('/users.json') .then(function (res) { console.log(res.body); //=> { ... } });
Handling Requests
- url The resource URI
- method The HTTP request method (default:
"GET"
) - headers An object of HTTP headers, header name to value (default:
{}
) - query An object or string to be appended to the URL
- body An object, string or form data to pass with the request
- timeout The number of milliseconds before cancelling the request (default:
Infinity
)
Response Objects
Every Popsicle response will give a Response
object on success. The object provides an intuitive interface for requesting common properties.
- status An integer representing the HTTP response status code
- body An object (if parsable) or string that was the response HTTP body
- headers An object of lower-cased keys to header values
- statusType() Return an integer with the HTTP status type (E.g.
200 -> 2
) - info() Return a boolean indicating a HTTP status code between 100 and 199
- ok() Return a boolean indicating a HTTP status code between 200 and 299
- clientError() Return a boolean indicating a HTTP status code between 400 and 499
- serverError() Return a boolean indicating a HTTP status code between 500 and 599
- get(key) Retrieve a HTTP header using a case-insensitive key
- type() Return the response type (E.g.
application/json
)