Request: How to set user agent for every request?

Using request is it possible to set the user agent for every request?

Currently I have to set it at the time of making the request:

request.post(url, { form: form, headers: { 'User-Agent': ua }}, function(err, resp, body) {
    // Do some stuff
})

Is it possible?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can create a request method with your own defaults:

var customHeaderRequest = request.defaults({
    headers: {'User-Agent': ua}
})

Then use it:

customHeaderRequest.post(url)

see https://www.npmjs.com/package/request#requestdefaultsoptions

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply