Skip to content
Fetch for ThinkJS 3.x
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Update the api interface, use fetch instead of request Apr 12, 2017
.eslintrc update eslint rule Jul 16, 2017
.travis.yml update .travis.yml Apr 10, 2017
index.d.ts add typescript extend definition Nov 30, 2017
package.json add typescript extend definition of the package.json Nov 30, 2017


npm Build Status Coverage Status

Fetch for ThinkJS 3.x


$ npm install think-fetch --save

How to use

config file src/config/extend.js

const fetch = require('think-fetch');

module.exports = [
  fetch, // HTTP request client.

Methods in Controller

module.exports = class extends think.Controller {
  async indexAction () {

    // plain text or html
    const text = await this.fetch('').then(res => res.text());

    // json
    const json = await this.fetch('').then(res => res.json());

    // post
    const body = await this.fetch('', { method: 'POST', body: 'a=1' }).then(res => res.json());

    // stream
    const res = await this.fetch('');
    const dest = fs.createWriteStream('./octocat.png');

    // post with stream from file
    const stream = fs.createReadStream('input.txt');
    const result = this.fetch('', { method: 'POST', body: stream }).then(res => res.json());


fetch(url[, options])

  • url A string representing the URL for fetching
  • options Options for the HTTP(S) request
  • Returns: Promise<Response>

Perform an HTTP(S) fetch.

url should be an absolute url, such as A path-relative URL (/file/under/root) or protocol-relative URL (// will result in a rejected promise.


The default values are shown after each option key.

  // These properties are part of the Fetch Standard
  method: 'GET',
  headers: {},        // request headers. format is the identical to that accepted by the Headers constructor (see below)
  body: null,         // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
  redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect

  // The following properties are node-fetch extensions
  follow: 20,         // maximum redirect count. 0 to not follow redirect
  timeout: 0,         // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
  compress: true,     // support gzip/deflate content encoding. false to disable
  size: 0,            // maximum response body size in bytes. 0 to disable
  agent: null         // http(s).Agent instance, allows custom proxy, certificate etc.
Default Headers

If no values are set, the following request headers will be sent automatically:

Header Value
Accept-Encoding gzip,deflate (when options.compress === true)
Accept */*
Connection close (when no options.agent is present)
Content-Length (automatically calculated, if possible)
User-Agent node-fetch/1.0 (+

More Methods Click

You can’t perform that action at this time.