简体   繁体   中英

Python Request input is invalid

I'm trying to make a python POST request, based on data that I get from a separate GET request.

Get request functions properly, and I get a response that looks like:

{
 "meta": {}, 
 "linked": {}
 "data": [
    {
      "date_on_hold": null, 
      "cc": [], 
      "agent": 8, 
      "person": 210
      "fields": {
           "1": {
                "value": "nellson.dom", 
                "aliases": []
        } 
    }
]

I put some of that data into a few lists (omitted) but when I try to perform a POST request like this:

data = {"subject": "Testing POST request", "person": 210, "agent": 8, "[fields][1][data]": "nellson.dom" }
data = json.dumps(data)

postResponse = requests.post(url=URL, headers=headers, data=data)
print(postResponse.status_code)
print(postResponse.text)

I get a 400 error code, with message: "Unexpected field names: "[fields][1][value]""

Am I doing something wrong here? How might I go about posting the data at [data][fields][1][value]?

I want to add, that if I got rid of that particular field in the data section so it would look like:

data = {"subject": "Testing POST request", "person": 210, "agent": 8}

Everything functions properly.

  1. "[fields][1][data]" at the POST request is used as a key, and it's considered as a string since it's inside double quotes, but this isn't what you want.

  2. If you want to get the "value" key which inside the first dict of the "fields" key to be used as a param key at your POST request, check the following key matching:

  3. Regarding this is the GET response

get_response = {
    "meta"  : {},
    "linked": {},
    "data"  : [
        {
            "date_on_hold": null,
            "cc"          : [],
            "agent"       : 8,
            "person"      : 210,
            "fields"      : {
                "1": {
                    "value"  : "nellson.dom",
                    "aliases": []
                }
            },
        }
    ]
}

  1. Your data dict for the POST request should be as the following:
data = {"subject": "Testing POST request", "person": 210, "agent": 8, list(get_response["data"][0]["fields"]["1"].keys())[0]: "nellson.dom" }

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