psf / psf/requests

The iterable produced by `Session.resolve_redirects` does not include the very first response

Open
#1,953 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Request
Dominant language
Python
Stars
54.3k
Forks
10.5k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

If you are manually walking over redirects, you probably want to structure your code like this:

redirect_sequence = session.send_with_manual_redirect_walking(request, ...)
for resp in redirect_sequence:
    # do something with 'resp'

The existing API does not let you do that. You must write either

first_response = session.send(request, ..., allow_redirects=False)
# do something with 'first_response'
for resp in session.resolve_redirects(first_response, request, ...)
    # do something with 'resp'

which involves writing the same "do something with" code in two places, or

resp = session.send(request, ..., allow_redirects=False)
redir_iter = session.resolve_redirects(resp, request, ...)
while True:
    # do something with 'resp'
    if not resp.is_redirect: break
    resp = next(redir_iter)

which is un-Pythonic loop structure.

Since Session.resolve_redirects must remain as is for compatibility's sake, the only way to fix this is to add either a new mode to send (allow_redirects=MANUAL?) or a new Session method (perhaps in fact called send_with_manual_redirect_walking) which returns an iterable that does include the very first response. I do not particularly care which, or what the new method is called in the second case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Session.send and Session.resolve_redirects, focusing on how redirect responses are exposed and why resolve_redirects must remain compatible. Choose and specify the new manual redirect-walking API, then verify that its iterable includes the initial response while preserving existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.