-1

Lets say I have this json:

{'name': 'jon',
 'id' : '20304',
 'pets': [{'name': 'sparkles',
           'age': 10
           'stats': {'weight':10,
                     'favetoy':'cottonball'
                     'color': 'green'}
          }]
}

Im trying to modify the 'color' parameter inside stats in a PUT request, by doing something like:

url = "https://....." #hostname+api url
payload = {
        'pets': [{'stats:{'color':'red}}]           
}
response = requests.put(url, json=payload, headers=headers)
 #assume headers defined elsewhere.

And while it is changing the color to red, it's also changing the whole 'pets' array (name, age) to nothing, which makes sense, since my request body doesnt include anything for the other params within the array. But I just want to change the color to red, while keeping the rest the same...but I don't see how since its nested within the array... what would I include in the request body instead?

TLDR: I just want to make an api put request to modify 'color' and keep everything else the same in the same way you do with 'id' or 'name'.

Any ideas??

  • According to [this](https://stackoverflow.com/a/50257328/6298712) answer, PUT is for replacing a resource, not updating it. Look into PATCH, or just send the entire thing back maybe? Seems that `PUT` is generally considered to replace, so your API may be expecting a full record. – ddejohn Jun 17 '22 at 21:43
  • 2
    Do you already have access to the original JSON, and if so, are you supposed to send the entire JSON back? Or do you just need to specify a field to change? It may also be difficult for us to provide answers if this behavior is very API-specific (e.g. do you have any docs you can reference)? – wkl Jun 17 '22 at 21:43
  • This depends *entirely* on how the API in question has implemented the functionality behind each of these HTTP verbs for the specific endpoint you're leveraging. Since you haven't provided enough requisite detail about the target API, this question isn't possible to answer in its current state. See also: [ask] – esqew Jun 17 '22 at 21:46

2 Answers2

0

treat payload as dictionary and make calls in that fashion, here the call will be like

payload["pets"][0]["stats"]["color"] = "red"#the changed color the [0] is there as there is alist
0

According to REST principles, to transmit only the parameters you want to change you should use the method PATCH, not PUT.

PATCH is used to modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource.

This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch.

Whether it's actually possible to do that in your case depends on the api you are working with. If it supports PATCH, check the api documentation to find out how to use it.

Otherwise you're stuck with retrieving the entire object, making the changes you want, and using PUT to commit the modified version of the entire object.

alexis
  • 48,685
  • 16
  • 101
  • 161