我正在尝试使用请求python 2.7进行curl post查询,但是使用curl与请求lib的API响应不同。
post查询非常简单,文件和名称 - 值对数据就像API params。
以下是curl multipart post请求:
curl -uadmin:blabla123 -X POST 127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/6a0ab661-1c43-43ed-b07f-a564f6bcb5ca/children -F filedata=@file1.txt -F name=document__55;nodeType=content
python 2.7代码如下:
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from config import USER, PASSWD
def createDocument( documentFilename, documentMetadata, targetFolderNodeId):
'''
Uploads a file and its meta-data to the CMIS server under the specified
target folder
'''
with open(documentFilename, 'rb') as file:
files = {'file': file}
# createURL = 'http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/{0}/children'.format( targetFolderNodeId )
createURL = 'http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/6a0ab661-1c43-43ed-b07f-a564f6bcb5ca/children'
data = {
"name":"document__55",
"nodeType":"cm:content",
}
response = requests.post( createURL, data = data, files = files, auth=HTTPBasicAuth(USER, PASSWD) )
print(response)
print(response.json)
print(response.text)
createDocument('file1.txt', '', '')
Curl返回200个http代码,但脚本奇怪地返回400.
任何帮助都非常感激。
答案 0 :(得分:0)
首先,这个剧本失败和卷曲工作的原因可能不止一个:
IList<string> strs = new List<string>();
IList<object> objs = new CovariantReadOlyList<string, object>(strs);
,而脚本中的nodeType=content
"nodeType":"cm:content"
,而multipart/form-data
的{{1}}选项实际上是此外,端点应提供详细错误以及400响应。如果是,那可能有助于识别错误原因。
答案 1 :(得分:0)
实际上在卷曲中我有-F filedata=@file1.txt但是在脚本中我有文件= {&#39;文件&#39;:文件}这是不一样所以我只需要使用文件= {&#39; filedata&#39;:文件}:)
def createDocument( documentFilename, documentMetadata, targetFolderNodeId):
'''
Uploads a file and its meta-data to the CMIS server under the specified
target folder
'''
with open(documentFilename, 'rb') as file:
files = {'filedata': file}
createURL = 'http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/{0}/children'.format( targetFolderNodeId )
data = {
"name":"document__77",
"nodeType":"cm:content"
}
response = requests.post( createURL, data = data, files = files, auth=HTTPBasicAuth(USER, PASSWD) )
print(response)
print(response.json)
print(response.text)
print(response.headers)
# print([i for i in dir(response) if 'header' in i])