用于Node和瀏覽器的簡單的HTTP請求:Popsicle

jopen 10年前發布 | 11K 次閱讀 Popsicle JavaScript開發

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)
  • </ul>

    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)

      項目主頁:http://www.baiduhome.net/lib/view/home/1416984438346

     本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!