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.

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

From: Aleksandar Lazic via curl-users <curl-users_at_cool.haxx.se>
Date: Tue, 15 Jun 2021 22:54:01 +0200

On 10.06.21 09:48, Daniel Stenberg via curl-users wrote:
> 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?


How about to use "curl_getenv()" which is already available and wrap it into a
--data-functions function.
https://github.com/curl/curl/blob/master/docs/libcurl/curl_getenv.3

curl --data-functions "{string: env(name)}" $URL

The env function then always return "curl_getenv(name)" with quotes.
This opens a possible feature set for further data functions.

Jm2c.

Regards
Alex

-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2021-06-15