{"id":"3b6a4f4fd493451b","repo":"urllib3/urllib3","slug":"body-pos-must-be-of-type-integer-instead-it-was","errorCode":null,"errorMessage":"body_pos must be of type integer, instead it was {type(body_pos)}.","messagePattern":"body_pos must be of type integer, instead it was (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/urllib3/util/request.py","lineNumber":221,"sourceCode":"\n    :param int pos:\n        Position to seek to in file.\n    \"\"\"\n    body_seek = getattr(body, \"seek\", None)\n    if body_seek is not None and isinstance(body_pos, int):\n        try:\n            body_seek(body_pos)\n        except OSError as e:\n            raise UnrewindableBodyError(\n                \"An error occurred when rewinding request body for redirect/retry.\"\n            ) from e\n    elif body_pos is _FAILEDTELL:\n        raise UnrewindableBodyError(\n            \"Unable to record file position for rewinding \"\n            \"request body during a redirect/retry.\"\n        )\n    else:\n        raise ValueError(\n            f\"body_pos must be of type integer, instead it was {type(body_pos)}.\"\n        )\n\n\nclass ChunksAndContentLength(typing.NamedTuple):\n    chunks: typing.Iterable[bytes] | None\n    content_length: int | None\n\n\ndef body_to_chunks(\n    body: typing.Any | None, method: str, blocksize: int\n) -> ChunksAndContentLength:\n    \"\"\"Takes the HTTP request method, body, and blocksize and\n    transforms them into an iterable of chunks to pass to\n    socket.sendall() and an optional 'Content-Length' header.\n\n    A 'Content-Length' of 'None' indicates the length of the body\n    can't be determined so should use 'Transfer-Encoding: chunked'","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/urllib3/urllib3/blob/c8d039c1b743f0bf5ee136972c68350fd91d41f1/src/urllib3/util/request.py#L203-L239","documentation":"Raised as ValueError from rewind_body() when body_pos is neither an int nor the _FAILEDTELL sentinel (and no seek method exists, or the type simply doesn't match). This is essentially a programming/API-contract error: callers of this internal helper must pass an integer byte offset.","triggerScenarios":"Calling urllib3.util.request.rewind_body() directly with a float, str, None, or other non-int position; passing a stale body_pos that was serialized/deserialized and lost its type; internal bookkeeping that loses the int type.","commonSituations":"User code reaching into urllib3 internals (rewind_body is module-level) instead of using the public request API; type drift after JSON-serializing request state; monkeypatching that injects a wrong-typed position.","solutions":["Don't call rewind_body() directly — let urllib3's request machinery manage body positioning during retries/redirects.","If you must, ensure body_pos is an int: rewind_body(body, int(body_pos)).","Validate upstream: if body_pos is None or not isinstance(body_pos, int), skip the rewind path."],"exampleFix":"// before\nrewind_body(body, body_pos='0')  # ValueError: body_pos must be of type integer\n\n// after\nrewind_body(body, int(body_pos))","handlingStrategy":"type-guard","validationCode":"assert isinstance(body_pos, int), f'body_pos must be int, got {type(body_pos)}'","typeGuard":"from urllib3.util.request import _FAILEDTELL\ndef is_valid_pos(pos) -> bool:\n    return isinstance(pos, int) or pos is _FAILEDTELL","tryCatchPattern":"try:\n    rewind_body(body, body_pos)\nexcept ValueError:\n    rewind_body(body, int(body_pos))","preventionTips":["Avoid calling rewind_body directly; use the public request API","Keep body_pos as int through serialization boundaries"],"tags":["http","validation","request-body","internals"],"analyzedSha":"c8d039c1b743f0bf5ee136972c68350fd91d41f1","analyzedAt":"2026-08-04T20:12:33.219Z","schemaVersion":2}