Additional Headers in PUT Request

Hello,

I’m automating Api Requests with RequestsLibrary and noticed additional headers in logs when using PUT:

 headers={'User-Agent': 'python-requests/2.32.3', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '51'} 

where i put only

 'Authorization': 'Bearer <TOKEN>', 'Content-Type': 'application/json', 

Is there a way to disable those headers?

Hi Tomasz,

These are the default headers (they come from the underlying python requests module.

First thing I’d try is define the headers you don’t want with a value of None, with a bit of luck that will un-define them :crossed_fingers:

I’ve never done it but if you use PUT On Session you may be able to use Update Session to remove them before the PUT.

If you are using PUT, maybe not because the requests module creates a temporary session to make the PUT call, and that temporary session creates the default headers.

I found this example of removing the default headers in python for the requests module, so it’s possible in the underlying code, just not sure if there’s anything implemented in this library.

Hopefully some else can give you a better answer,

Dave.

What is the problem you’re running into? It’s typical for frameworks / libraries / browsers / etc. to inject headers. What issue are you trying to resolve?

2 Likes

Some of those are necessary for the backend endpoint to respond correctly to your api call.

  • User-agent: not required
  • Accept-Encoding: not required but indicates to the server if it can compress the response to avoid sending the response in plain-text. This is not required but if client provides this, backend server can optimize the amount of data that actually gets transfered back to you as reply to your call.
  • Connection: This typically indicates that client can re-use the same socket for consecutive requests and server now knows this. If used without keep-alive, every consecutive request you make against the same server will close the tcp socket and re-open it again if new request is done. This will slow down the overall performannce. So, at least adviced to use it.
  • Accept: this tells the backend server what format the client accepts the response in. Might not be required and since this is wild-card “accept anything”, most likely not necessary. Most of the real-world scenarios use same value here as Content-type
  • Content-Length: required but its presence depends on what http method are are using. Since you are putting a json, backend server needs to know the lenght of data you are sending and requests calculates that for you so that you do not need to do it manually.

So in general, all of those headers except user-agent do serve an actual purpose for your client and backend communicating and removing all of them sounds like a bad idea.

2 Likes

Hello,

Thanks for help! :slight_smile:

I was trying to test what will happen when i remove the headers, check behavior. Nothing to fancy.

Setting None helped for what i was trying to achieve.

1 Like