HTTP POST 中的表单:Curl 到 pyCurl
Form in HTTP POST: Curl to pyCurl
我正在尝试转换以下 Curl 片段:
-F "model={
name: 'myapp',
targets: [ { identity: [ 'servers', 'server_name' ] } ]
}" \
-F "sourcePath=@/deployments/MyApp/app/MyApp.ear"
进入 pyCurl:
targets_list = list()
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
model = json.dumps({"name": 'myapp', \
"targets": targets_list } )
sourcePath = "/deployments/MyApp/app/MyApp.ear"
send = [("model", model), ('sourcePath', \
(pycurl.FORM_FILE, sourcePath)),]
curl_agent.setopt(pycurl.HTTPPOST, send)
但我收到 500 HTTP 错误:
{
"detail": "JSONArray[0] is not a JSONObject.",
"status": 500
}
问题肯定出在 'targets' 列表上...有什么想法吗?
编辑:这是完整的 Python 脚本:
import pycurl, json
url = "http://localhost:7001/some_context"
agent = pycurl.Curl()
agent.setopt(pycurl.POST, 1)
targets_list = list()
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
model = json.dumps({"name": 'myapp', \
"targets": targets_list } )
sourcePath = "/deployments/MyApp/app/MyApp.earr"
send = [("model", model), ('sourcePath', \
(pycurl.FORM_FILE, sourcePath)),]
agent.setopt(pycurl.HTTPPOST, send)
agent.setopt(pycurl.URL, url)
agent.setopt(pycurl.HTTPHEADER, ['Accept: application/json',
'X-Requested-By:MyClient',
'Content-Type:multipart/form-data',
'Content-Length:'])
agent.setopt(pycurl.VERBOSE, 1)
agent.setopt(pycurl.USERPWD, "user:XXXXXX")
agent.perform()
好的。
应该是:
targets_list.append({ 'identity': ['servers', 'server_name'] })
而不是:
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
现在可以了:)
我正在尝试转换以下 Curl 片段:
-F "model={
name: 'myapp',
targets: [ { identity: [ 'servers', 'server_name' ] } ]
}" \
-F "sourcePath=@/deployments/MyApp/app/MyApp.ear"
进入 pyCurl:
targets_list = list()
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
model = json.dumps({"name": 'myapp', \
"targets": targets_list } )
sourcePath = "/deployments/MyApp/app/MyApp.ear"
send = [("model", model), ('sourcePath', \
(pycurl.FORM_FILE, sourcePath)),]
curl_agent.setopt(pycurl.HTTPPOST, send)
但我收到 500 HTTP 错误:
{
"detail": "JSONArray[0] is not a JSONObject.",
"status": 500
}
问题肯定出在 'targets' 列表上...有什么想法吗?
编辑:这是完整的 Python 脚本:
import pycurl, json
url = "http://localhost:7001/some_context"
agent = pycurl.Curl()
agent.setopt(pycurl.POST, 1)
targets_list = list()
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
model = json.dumps({"name": 'myapp', \
"targets": targets_list } )
sourcePath = "/deployments/MyApp/app/MyApp.earr"
send = [("model", model), ('sourcePath', \
(pycurl.FORM_FILE, sourcePath)),]
agent.setopt(pycurl.HTTPPOST, send)
agent.setopt(pycurl.URL, url)
agent.setopt(pycurl.HTTPHEADER, ['Accept: application/json',
'X-Requested-By:MyClient',
'Content-Type:multipart/form-data',
'Content-Length:'])
agent.setopt(pycurl.VERBOSE, 1)
agent.setopt(pycurl.USERPWD, "user:XXXXXX")
agent.perform()
好的。
应该是:
targets_list.append({ 'identity': ['servers', 'server_name'] })
而不是:
targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))
现在可以了:)