Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request: How to set user agent for every request?

Tags:

node.js

npm

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?

like image 607
BugHunterUK Avatar asked Jun 28 '16 10:06

BugHunterUK


People also ask

How do I change the User-Agent in HTTP request?

To set a custom user agent, include an agent string in the HTTP header User-Agent. For the integration name, use a string that clearly and meaningfully identifies your integration. For the integration version, use a build ID, commit hash, or other identifier that is updated when you release new integration versions.

How do I change User-Agent in Python request?

Use requests. get() to set the user agent Define a dictionary with a key "User-Agent" set to the desired user agent. Call requests. get(url, headers=headers) with url as the target URL and headers as the dictionary of the previous step to set the user agent in a request.

What is the default User-Agent of Python requests?

1) the default user agent is python-requests/2.25.


1 Answers

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

like image 101
Simon Hänisch Avatar answered Sep 23 '22 19:09

Simon Hänisch