1

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 Answer 1

1
  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" }
Sign up to request clarification or add additional context in comments.

2 Comments

What if I already have the value of "nellson.dom" stored in a separate list? Would I still be able to do this? I have the data for "person", "agent", and "[fields][1][value" stored in separate lists from my GET request, then I'm passing those lists to a separate script for the POST request.
@NelsonSwasono Yes, you would still be able to do this if "nellson.dom" was in a separate list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.