Alex Zaitsev
Alex Zaitsev

Reputation: 701

How sending files via Python Requests library is different to Curl?

I need to upload file to Facebook via API. To do it i tried to use Curl and everything works great:

curl -F 'source=@/file.mp4' -F 'access_token=secret' https://graph.facebook.com/v4.0/act_000042/advideos

Also i'm trying implement the same in Python using Requests:

    import requests # requests==2.19.1

    with open('/file.mp4', 'rb') as filecontent:
    response = requests.post(
       'https://graph.facebook.com/v4.0/act_000042/advideos',
        data={
            'access_token': 'secret',
        },
        files={
           'source': filecontent,
        }
    )

And i get the same error: {'error': {'code': 1, 'message': 'An unknown error occurred'}. So there is some difference between how Curl uploads files and how Requests uploads them.

What is the difference and how can i implement the same download via Requests?

Upvotes: 3

Views: 150

Answers (1)

Alex Zaitsev
Alex Zaitsev

Reputation: 701

Ok, so i figured out the difference is in Content-Disposition. Curl does not add anything if filename contains utf-8 symbols and content-disposition looks like:

Content-Disposition: form-data; name="source"; filename="someunicodename..."

Requests makes content-disposition according https://greenbytes.de/tech/webdav/rfc5987.html and it looks like:

Content-Disposition: form-data; name="source"; filename*=utf-8''someunicodename ...

Facebook API does not understand rfc5987 specs and consider this content-disposition as inappropriate. Using only ASCII symbols in file name did solve the problem.

Upvotes: 3

Related Questions