superagent icon indicating copy to clipboard operation
superagent copied to clipboard

using a custom http.Agent (extend documentation)

Open stixx200 opened this issue 6 years ago • 3 comments

I was searching for a solution to use a custom http.Agent for every request. In early 3.x releases, it was possible to do something like (ref: #1130):

const http = require('http')
const request = require('superagent')

const myAgent = new http.Agent()

request(method, url).agent(myAgent)

With the PR #1302 the .agent() method only accepts some options, but no http.Agent instance.

My solution was to inject a middleware function with .use(...). Is there any better solution for this scenario? I would suggest to extend the documentation about using custom http.Agent and superagent.agent().use().

My solution:

const myAgent = new http.Agent()
const request = superagent.agent().use(req => req.agent(myAgent))

stixx200 avatar Feb 07 '20 07:02 stixx200

@niftylettuce, could you please clarify what is expected/good way to set custom agent?

My use-case: I would like to reuse connections by using agent with keepAlive: true - see `https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_new_agent_options

matej-prokop avatar Mar 31 '20 08:03 matej-prokop

I would love to know what the proper way is to use keep alive with superagent also. I used stixx200's solution but there's got to be a cleaner way to do it.

shanebo avatar Sep 09 '22 21:09 shanebo

Same here. I would love to have details about connection reuse, keepAlive, and how to use à custom Agent instance.

Galienna avatar May 17 '23 12:05 Galienna