nicolasff / webdis

A Redis HTTP interface with JSON output
https://webd.is
BSD 2-Clause "Simplified" License
2.82k stars 307 forks source link

Reading data from json in request body #200

Closed CatAtGat closed 2 years ago

CatAtGat commented 2 years ago

Hello, I've read through the documentation and issues (both open and closed) and I haven't yet found my answer.

I'm trying to send webdis a command which will essentially do the following

http://webdis.server/set/keyOfData

and then set that data equal to a large hexBinary json element in the request body:

{ "data" : "3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f" }

The hexBinary data could be larger than is possible within a URL encoded request, so I need a way to set the data in Reddis from the json body.

Any help is much appreciated.

nicolasff commented 2 years ago

Hello,

You can send a PUT request, as documented in the README in the "file upload" section. There is also a test uploading a 10,000-byte file and verifying the stored length with STRLEN: curl-tests.sh.

CatAtGat commented 2 years ago

This does seem to be a step towards my solution. As the documentation sates, it sets the value to the entirety of the body, this isn't precisely what I want as it then returns:

{ "hexBinary": "{\"data\":\"3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f\"}", "isEmpty": false }

When what I want is:

{ "hexBinary" : "3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f", "isEmpty" : false }

Perhaps I'll implement a custom command which shaves the characters off the front and tail of the string.

Thanks.

nicolasff commented 2 years ago

Webdis is just a bridge between an HTTP client and Redis, so it doesn't expect payloads to have a specific JSON format from which it would extract a specific part to send to Redis. It takes exactly what you sent and passes it to Redis, just with an HTTP interface. Webdis completely ignores any formatting that the contents of your request might have: the example given in the README demonstrates this by using a PNG file which is stored and retrieved without changing its hash (so it's exactly the same), but it could also be a well-formed JSON file or a malformed one… it's just taken as-is and used as the last parameter to the command in the URL.

So yes, you'll have to tweak what you write into Redis (via Webdis) if you want a specific format to be sent back.

That said, there's one thing I don't understand here: what's producing this JSON value with an isEmpty field set to false? I don't see how Webdis could be producing that unless it was part of the stored value itself.

CatAtGat commented 2 years ago

It's a second layer on top of Webdis, not Webdis itself. I ended up implementing the string trimmer on that second layer.