简体   繁体   中英

Twilio NodeJS - Request through Proxy

How do I specify proxy when using Twilio github library?

As a workaround I can manually make requests to twilio. Alternatively I can modify my local version of ./lib/RequestClient.js :

var options = {
    timeout: opts.timeout || 30000,
    proxy: 'http://myproxy.com:8080', // <- My proxy override
    followRedirect: opts.allowRedirects || false,
    url: opts.uri,
    method: opts.method,
    headers: opts.headers,
    forever: opts.forever === false ? false : true,
  };

Looking at twilio code - that request is not looking for proxy options. It would be nice if Twilio modified their RequestClient to accept proxy options during initialization.

Thank you!

Hey Twilio Developer Evangelist here.

Yes the helper library doesn't directly support proxies at the moment. There are a few options you could do to get this solved.

Option 1: The helper library is using the request module under the hood. Aside of the proxy option there is another way to specify a proxy using environment variables. If twilio is the only thing that is using request in your code base, that might be an option. You can read more about that in the request documentation: https://www.npmjs.com/package/request#controlling-proxy-behaviour-using-environment-variables

Option 2: The helper library allows you to specify your own RequestClient . It has to implement the same interface as the RequestClient in lib/base/RequestClient.js of the helper library. The easiest way would be to copy that file, modify the options variable to have the proxy option and then pass it to the Twilio constructor:

const twilio = require('twilio');
const MyRequestClient = require('./path/to/your/MyRequestClient');

const client = twilio(
    'account_sid',
    'auth_token', 
    { httpClient: new MyRequestClient() })

// do your requests

I hope this helps. I also made sure to create a GitHub issue to make sure that this is added in the long term as an option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM