Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of http proxy #79

Closed
jimliang opened this issue Feb 16, 2016 · 5 comments
Closed

Support of http proxy #79

jimliang opened this issue Feb 16, 2016 · 5 comments

Comments

@jimliang
Copy link

If I want to use http proxy,I will

 require('http').request({
    host:'127.0.0.1',
    port:8580,
    method:'POST',
    path:'https://www.google.com',
},function(res){
    //...
})

But I use node-fetch,

fetch(obj).then(function(res){
    ...
})

It throw TypeError: Parameter 'url' must be a string, not object

It does not support URL objects

@bitinn
Copy link
Collaborator

bitinn commented Feb 16, 2016

You can pass a http.Agent with custom proxy settings: https://github.com/bitinn/node-fetch#options

see also: #8 (comment)

@jimliang
Copy link
Author

It resolved, I used https://github.com/TooTallNate/node-https-proxy-agent

fetch('https://www.google.com',{ agent:new HttpsProxyAgent('http://127.0.0.1:8580')}).then(function(res){
    ...
})

@bitinn bitinn closed this as completed Feb 16, 2016
franktopel added a commit to franktopel/fontello-webpack-plugin that referenced this issue Feb 14, 2018
@jimliang
Copy link
Author

use https://github.com/zjael/simple-proxy-agent to support more proxy type. http、socks5.

@lisonge
Copy link

lisonge commented Oct 19, 2020

It resolved, I used https://github.com/TooTallNate/node-https-proxy-agent

fetch('https://www.google.com',{ agent:new HttpsProxyAgent('http://127.0.0.1:8580')}).then(function(res){
    ...
})

i use typescript, and it type is incompatible

import { HttpProxyAgent } from 'http-proxy-agent';
import fetch from 'node-fetch';

fetch('', {
    method: 'GET',
    agent: new HttpProxyAgent('http://127.0.0.1:10809')
})

Type 'HttpProxyAgent' is not assignable to type 'Agent | ((parsedUrl: URL) => Agent) | undefined'. Property 'freeSockets' is missing in type 'HttpProxyAgent' but required in type 'Agent'.

@kics223w1
Copy link

kics223w1 commented Sep 20, 2022

You can use package tunnel to support node fetch with proxy.
Example Implement:

   const tunnel = require('tunnel');  
   const fetch = require('node-fetch');
   import fs from 'fs';
  
    const configTunnel = {
      ca: [fs.readFileSync(
      "YOUR_CERTIFICATE_PATH"
    )],
      proxy: {
        host: this.currentIP,
        port: proxyPort,
      },
    };

    const tunnelHTTPS = tunnel.httpsOverHttp(configTunnel);

    const tunnelHTTP = tunnel.httpOverHttp(configTunnel);

    let config: Record<string, any> = {
      url: flow.request.url,
      method: method,
      headers: headers,
      agent: flow.isSSL ? tunnelHTTPS : tunnelHTTP,
    };
    if (flow.request.bodySize > 0 && method !== 'GET' && method !== 'HEAD') {
      config.body = flow.request.body;
    }

   try {
      const response = await fetch(flow.request.url, config);
    } catch (e: any) {
      throw new Error(e.message);
    }

Note that: node fetch with proxy doesn't work if Method GET contains body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants