3

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?

5
  • Using data= leads to post form, i assume access_token have to be in headers= or in files=. Read post-a-multipart-encoded-file Commented Sep 20, 2019 at 17:29
  • @stovfl the same method works well for image upload Commented Sep 20, 2019 at 17:42
  • 'An unknown error occurred': The error message tells nothing, are you sure the endpoint v4.0/act_000042/advideos is valid and accepts .mp4. I'm missing any Content-Type also. Commented Sep 20, 2019 at 17:48
  • "What is the difference": I have compared both, curl, requests.post, header and fields and found no differences. Commented Sep 20, 2019 at 18:36
  • I don't know graph.facebook.com, are you sure it accepts APIs? It might just be blocking you: robots.txt. Commented Sep 21, 2019 at 17:40

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.