Skip to content

How to add custom headers to the reader? #337

Answered by lemon24
Scylla2020 asked this question in Q&A
Discussion options

You must be logged in to vote

Check out SessionWrapper.request_hooks and RequestHook:

>>> from reader import make_reader
>>> reader = make_reader('')
>>> reader.add_feed('http://localhost:8080')
>>> 
>>> def hook(session, request, **kwargs):
...     request.headers.setdefault('custom', 'header')
... 
>>> reader._parser.session_factory.request_hooks.append(hook)
>>> reader.update_feed('http://localhost:8080')

Note the custom header received by nc:

 $ echo 'HTTP/1.1 304' | nc -l localhost 8080
GET / HTTP/1.1
Host: localhost:8080
User-Agent: python-reader/3.13.dev0 (+https://github.com/lemon24/reader)
Accept-Encoding: gzip, deflate
Accept: application/atom+xml,application/rdf+xml,application/rss+xml,application/x-netcdf,…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Scylla2020
Comment options

Answer selected by Scylla2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #336 on June 15, 2024 13:03.