Skip to content
Discussion options

You must be logged in to vote

I think the problem is that requests just ignores the file parameter that is None and sends the request without it.
You can use the code below to see the raw request, that is sent:

import requests

def pretty_print_POST(req):
    print('{}\n{}\r\n{}\r\n\r\n{}'.format(
        '-----------START-----------',
        req.method + ' ' + req.url,
        '\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))

with open("a.txt", "rb") as f:
    req = requests.Request(
        'POST',
        "http://localhost:8080/test",
        files=(
            ("firstFile", ("a.txt", f, "application/octet-stream")),
            ("secondFile", None)
        ),

    )
  …

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@phuchm
Comment options

@YuriiMotov
Comment options

@phuchm
Comment options

@YuriiMotov
Comment options

Answer selected by YuriiMotov
@phuchm
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #12039 on August 19, 2024 11:18.