简体   繁体   中英

Form in HTTP POST: Curl to pyCurl

I am trying to transform following Curl fragment:

-F "model={
name: 'myapp',
targets: [ { identity: [ 'servers', 'server_name' ] } ]
}" \
-F "sourcePath=@/deployments/MyApp/app/MyApp.ear"

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

But I am getting 500 HTTP Error:

{
    "detail": "JSONArray[0] is not a JSONObject.", 
    "status": 500
}  

The problem is definitely with the 'targets' list... Any ideas?

EDIT: Here's the full Python script:

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

Allright.

It should be:

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

Instead of:

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

Now it works :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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