Nick L
Nick L

Reputation: 171

How to use Python Requests to make this POST request

I'm trying to POST to the Adobe Sign API. Even if you're not familiar with the API, this should be a pretty straightforward question.

Sign's documentation says the POST I need to do looks like this:

POST /api/rest/v5/transientDocuments HTTP/1.1
Host: api.na1.echosign.com
Access-Token: MvyABjNotARealTokenHkYyi
Content-Type: multipart/form-data
Content-Disposition: form-data; name="File"; filename="MyPDF.pdf"

<PDF CONTENT>

Here is my current code, using Python Requests:

def createTransientDocument(your_file_base_url, file_name):
    headers = {'access-token': datafile.accessToken,
               'x-user-email': '<redacted for StackOverflow>',
               'content-type': 'multipart/form-data'}

    files = {'file': (file_name, open(your_file_base_url + file_name, 'rb'))}

    r = requests.post(datafile.transient_documents_URL, files = files, headers=headers)

    return r

Unfortunately, this is not quite what the API is looking for. I get a response:

*{"code":"BAD_REQUEST","message":"The request provided is invalid"}*

Any tips on how to use the Requests library to POST my PDF file to this API?

Sorry for the n00b question, but I am learning the Requests library right now. Thanks.

Upvotes: 0

Views: 342

Answers (1)

Nick L
Nick L

Reputation: 171

It's a nuance, but you need to name the file "File" with a capital F. Otherwise the API doesn't pick it up on the Adobe Sign side. Here is the correct (and working) API call.

def createTransientDocument(your_file_base_url, file_name):
    headers = {'access-token': datafile.accessToken,
               'x-user-email': '<email redacted>',
               'content-disposition':'form-data'}

    files = {'File': open(your_file_base_url+file_name, 'rb')}


    r = requests.post(datafile.transient_documents_URL, files=files, headers=headers)

    return r

Upvotes: 1

Related Questions