splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

Add ability to send custom `PUT` and `PATCH` requests with JSON body

Open moschi opened this issue 2 years ago • 8 comments

Bug description We have a REST endpoint to update a datastructure that requires the use of the HTTP PUT method. We're using request from the Context class to send a request to that endpoint. On binding.py#L876, the _encode method is called, whose result is used to overwrite the original body parameter of the method. This causes the request-body to be url-encoded and url-formatted.

Steps to reproduce the behavior:

Send a HTTP PUT request with a JSON body to an endpoint, using the request method of the Context class.

Expected behavior

The request method correctly escapes a JSON body, or provides instructions on how to call it to prevent the URL-encoding.

Logs or Screenshots

Currently, our REST endpoint receives a payload similar to this:

event=%7B%27_key%27%3A+%2766548f65179fc9792600b193%27%2C+%27tenant_uid%27%3A+%27default%27%7D&update_objects=%7B%27attribute%27%3A+status%2C+%27value%27%3A+%2766436bc36265a3697e049fe6%27%7D&comment=Updated

which does not decode to a valid JSON structure:

event={'_key':+'66548f65179fc9792600b193',+'tenant_uid':+'default'}&update_objects={'attribute':+status,+'value':+'66436bc36265a3697e049fe6'}&comment=Updated

Splunk:

  • Version: Enterprise 9.1.1
  • OS: RHEL 8.8 (Ootpa)
  • Deployment: single-instance

SDK:

  • Version: 1.7.4, bug persists in 2.0.1
  • Language Runtime Version: python 3.7
  • OS: RHEL 8.8 (Ootpa)

moschi avatar May 27 '24 14:05 moschi

Hi team!

Could we please get a response on this? It seems to be a minor fix!

Thank you!

seiimonn avatar Aug 19 '24 09:08 seiimonn

Dismissed as stale. Please tell us if the issue persists!

Ickerday avatar Sep 08 '25 17:09 Ickerday

@Ickerday, this is not stale and still makes using the SDK infeasible.

seiimonn avatar Sep 09 '25 07:09 seiimonn

Thanks @seiimonn, we're taking a look!

Ickerday avatar Sep 09 '25 08:09 Ickerday

Could you provide us with a complete reproducer?

From the description i assume you are doing something like:

service.request("test", method="PUT", body={"key": json.dumps({"key": 1})})

I observe that in such case we are sending following HTTP body:

key=%7B%22key%22%3A+1%7D

Which decodes successfully:

from urllib.parse import parse_qs

out = parse_qs("key=%7B%22key%22%3A+1%7D")
for k, v in out.items():
    for v in v:
        print(k, json.loads(v))

mateusz834 avatar Sep 09 '25 08:09 mateusz834

The original issue description is a bit imprecise. The post method of the service allows sdk users to pass the body param as a string - this leads to the service class skipping the encoding, as opposed to automatic encoding if a dict is passed. However, the request method (as a stand-in for the missing put and patch method) does not behave the same way. It would force us to check if we're working with form-data or with stringified JSON in our script endpoints.

Our desire is for the sdk API to be persistent so that we can use put and patch the same way we can use post - with stringified JSON, which we escape ourselves. You can find an implementation for put and patch here: https://github.com/splunk/splunk-sdk-python/pull/586

If adding put and patch to the sdk is not an option, a check similar to the one in post before encoding the body in the generic request method would be feasible as well.

moschi avatar Sep 09 '25 13:09 moschi

So we plan to support both ways to make it consistent:

I sent: #683 (which add more tests and implements the logic in the request method).

We also plan to go with #586, so that we are consistent. We are waiting for @seiimonn to get that change rebased on top of the develop branch.

mateusz834 avatar Nov 05 '25 11:11 mateusz834

Hi @mateusz834 - did the rebase!

seiimonn avatar Nov 06 '25 10:11 seiimonn