Python Requests HTTP 307 Stream Replay
Asked of: Software Engineer
Last updated
What's being tested
These debugging questions test tracing HTTP 307 redirect semantics through Python requests, especially preserving the method and replaying a streamed request body. You must reason from bytes on the wire—stream position, Content-Length, socket reads, and timeout behavior—then propose a minimal, robust fix for seekable versus non-replayable bodies.
Patterns & templates
-
307 preserves method and body: the redirected request remains
POST; unlike303, it must resend the original payload to the new URL. -
Seekable stream replay: record the initial position with
body.tell(), then callbody.seek(position)before retrying or redirect replay. -
requestsmay rewind using its internal body-position metadata; verifyPreparedRequest._body_positionand inspectrequests/urllib3behavior rather than assuming automatic replay. -
Content-Lengthframing must match bytes actually available; a reused stream at EOF can advertiseNbytes while sending zero, causing the server to wait. -
Prefer a bytes/string body for small payloads: it is naturally replayable; use a reopened file or seekable object for large uploads.
-
Treat generators and pipes as non-replayable unless buffered; retrying them can duplicate, truncate, or silently alter the payload.
-
Debug with a short
timeout, request/response logging,tell()before and after sends, and a local redirect server; distinguish client hang from server processing.
Common pitfalls
Pitfall: Saying “307 changes the request to
GET” confuses it with303;307preserves both method and body.
Pitfall: Rewinding only after the timeout is insufficient if the first request already consumed the stream; restore the position before the redirected send.
Pitfall: Recommending blind retries ignores non-seekable bodies and duplicate side effects; use idempotency keys or explicit buffering where appropriate.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Debug a POST With a Stream Body That Hangs After a 307 Redirect in Python RequestsStripe · Software Engineer · Technical Screen · medium
- Fix Seekable POST Body Replay After a 307 Redirect in Python requestsStripe · Software Engineer · Onsite · medium
- Debug an HTTP Client That Hangs After a 307 Redirect With a Streamed Request BodyStripe · Software Engineer · Technical Screen · hard
Related concepts
- Robust Networking, REST, And Concurrency ControlSystem Design
- Streaming, Large Inputs, And External MemorySoftware Engineering Fundamentals
- HTTP API Crawling and URL MazesCoding & Algorithms
- Concurrent Web Crawlers and Work QueuesSystem Design
- Web Crawlers, URL Normalization, And PolitenessSystem Design
- RESTful API And HTTP Service DesignSoftware Engineering Fundamentals