curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

[brain-storm] Can we make JSON posting easier?

From: Daniel Stenberg via curl-users <curl-users_at_cool.haxx.se>
Date: Thu, 10 Jun 2021 09:48:57 +0200 (CEST)

Hi,

I see a lot of users (on stackoverflow and elsewhere) use curl to send JSON.
Such users often want to pass in environment variables into the JSON and they
need to use double-quotes within the content to make the JSON syntactically
correct.

This makes it a quoting challenge. You can't use single-quotes around the
whole thing as then the environment variables won't work etc. Not to mention
what happens if the variable itself expands to something that contains a
quote...!

Can we introduce a way to make this easier for future curl users?

Very basic example:

   curl -d "{string: \"$name\"}" $URL

I have to idea embryos to kick off this with:

A) data-tr

Would it help if we can provide a tr-like replacement for the data string so
that we can make use of another character instead of double-quote on the
command line and have it replaced on the fly by curl? Imaginary example that
will replace exclamation marks with double-quotes in the data before it gets
sent off. The $name variable is an environment variable.

   curl --data-tr '!"' curl -d "{string: !$name!}" $URL

B) data-expr

Would it help if we introduce "functions" within the data content that can do
string magic like quoting for us? This is a modified --data option, the
--data-expr:

   curl --data-expr "{string: __quote__($name)}" $URL

The challenge there to come up with a way to do functions that isn't likely to
interfer with content and still is readable. Of course there's still a problem
here if $name expands to something with a close parenthesis in it...

Other ideas? Got any ways to refine/improve my two embryos here?

-- 
  / daniel.haxx.se
  | Commercial curl support up to 24x7 is available!
  | Private help, bug fixes, support, ports, new features
  | https://www.wolfssl.com/contact/
-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2021-06-10