HTTP POST中的表单:卷曲到pyCurl

时间:2016-03-31 09:58:09

标签: python curl http-post 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
}  

问题肯定在于'目标'列表......有什么想法吗?

编辑:这是完整的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()

1 个答案:

答案 0 :(得分:0)

奥莱特。

应该是:

targets_list.append({ 'identity': ['servers', 'server_name'] })

而不是:

targets_list.append(json.dumps({ 'identity': ['servers', 'server_name'] }, ensure_ascii=False))

现在有效:)