简体   繁体   English

python-requests和EchoSign

[英]python-requests and EchoSign

I am trying to post a file into EchoSign APIs and it works everywhere for me except for python-requests. 我正在尝试将文件发布到EchoSign API中,除了python-requests之外,它对我都有效。

I have here the CURL command that works perfectly 我这里有完美运行的CURL命令

curl -H "Access-Token: API_KEY" \
 -F File=@/home/user/Desktop/test123.pdf \
 https://secure.echosign.com/api/rest/v2/transientDocuments

and this is my requests function. 这是我的请求功能。 It will upload the PDF file but with garbage whereas CURL works perfectly. 它将上传PDF文件,但带有垃圾,而CURL可以完美运行。

api_url = 'https://secure.echosign.com/api/rest/v2'

def send_document(file_path, access_token=access_token):
    """Uploads document to EchoSign and returns its ID

   :param access_token: EchoSign Access Token
   :param file_path: Absolute or relative path to File
   :return string: Document ID

   """
    headers = {'Access-Token': access_token}

    url = api_url + '/transientDocuments'

    with open(file_path, 'rb') as f:
        files = {
            'File': f,
        }
        return requests.post(url, headers=headers, files=files).json().get('transientDocumentId')

What am I doing wrong?? 我究竟做错了什么?? I have tried posting the file as data instead of files too and still no different result 我尝试过将文件也作为数据而不是文件发布,仍然没有不同的结果

Thanks 谢谢

EDIT 编辑

It worked when I added 当我添加时它起作用了

data = {
    'Mime-Type': 'application/pdf',
    'File-Name': 'abc.pdf'
}

So, my new function would be: 因此,我的新功能将是:

def send_document(file_path, access_token=access_token):
    """Uploads document to EchoSign and returns its ID

    :param access_token: EchoSign Access Token
    :param file_path: Absolute or relative path to File
    :return string: Document ID

    """
    headers = {
        'Access-Token': access_token,
    }
    data = {
        'Mime-Type': 'application/pdf',
        'File-Name': 'abc.pdf'
    }
    url = api_url + '/transientDocuments'
    files = {'File': open(file_path, 'rb')}
    return requests.post(url, headers=headers, data=data,
                         files=files).json().get('transientDocumentId')

This is how it works 这是这样的

def send_document(file_path, access_token=access_token):
    """Uploads document to EchoSign and returns its ID

    :param access_token: EchoSign Access Token
    :param file_path: Absolute or relative path to File
    :return string: Document ID

    """
    headers = {
        'Access-Token': access_token,
    }
    data = {
        'Mime-Type': 'application/pdf',
        'File-Name': 'abc.pdf'
    }
    url = api_url + '/transientDocuments'
    files = {'File': open(file_path, 'rb')}
    return requests.post(url, headers=headers, data=data,
                         files=files).json().get('transientDocumentId')

Try passing in the filename and mime-type, like so: 尝试传入文件名和mime-type,如下所示:

files = {
    'File': (
        os.path.basename(file_path),
        f,
        'application/pdf',
    )
}

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM