请求和EchoSign

我正在尝试将文件发布到EchoSign API中,除了python-requests之外,它对我来说无处不在。

我在这里完美的CURL命令

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

这是我的请求功能。 它将上传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')

我究竟做错了什么?? 我试图将文件发布为数据而不是文件,并且仍然没有不同的结果

谢谢

编辑

它在我添加时起作用

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

所以,我的新功能是:

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')

这是它的工作原理

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')

尝试传入文件名和MIME类型,如下所示:

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

参考文献:

  • http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
  • man curl ,请参阅--trace-file FILE argumet
  • 适用于PDF文件的MIME媒体类型
  • 链接地址: http://www.djcxy.com/p/8195.html

    上一篇: requests and EchoSign

    下一篇: PHP upload fail for some PDF files (empty MIME type)