{"id":18389,"date":"2022-02-02T09:12:18","date_gmt":"2022-02-02T08:12:18","guid":{"rendered":"https:\/\/daniel.haxx.se\/blog\/?p=18389"},"modified":"2022-02-04T23:43:40","modified_gmt":"2022-02-04T22:43:40","slug":"curl-dash-dash-json","status":"publish","type":"post","link":"https:\/\/daniel.haxx.se\/blog\/2022\/02\/02\/curl-dash-dash-json\/","title":{"rendered":"curl dash-dash-json"},"content":{"rendered":"\n<p>The curl &#8220;cockpit&#8221; is yet again extended with a new command line option: <code><a href=\"https:\/\/curl.se\/docs\/manpage.html#--json\">--json<\/a><\/code>. The 245th command line option.<\/p>\n\n\n\n<p><a href=\"https:\/\/curl.se\/\">curl<\/a> is a generic transfer tool for sending and receiving data across networks and it is completely agnostic as to <em>what<\/em> it transfers or even <em>why<\/em>.<\/p>\n\n\n\n<p>To allow users to craft all sorts of transfers, or requests if you will, it offers a wide range of command line options. This flexibility has made it possible for a large number of users to keep using curl even as network ecosystems and its (HTTP) use have changed over time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"craft-content-to-send\">Craft content to send<\/h2>\n\n\n\n<p>curl offers a few convenience options for creating contents to send in HTTP &#8220;uploads&#8221; . Options such as <code>-F<\/code> for building <a href=\"https:\/\/everything.curl.dev\/http\/multipart\">multi-part formposts<\/a>, and <code>--data-urlencode<\/code> for <a href=\"https:\/\/everything.curl.dev\/http\/post\/url-encode\">URL-encoding POST data<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"json-is-the-new-popular-kid\">JSON is the new popular kid<\/h2>\n\n\n\n<p>When curl was born, JSON didn&#8217;t exist. Over the decades since, JSON has grown to become a very commonly used format for structured data, often used in &#8220;REST API&#8221; calls and more. curl command lines sending and receiving JSON are <em>very<\/em> common now.<\/p>\n\n\n\n<p>When asking users today what features they would like to see added in curl, and when asking them what good features they see in curl alternatives, a huge chunk of them mention JSON support in one way or another.<\/p>\n\n\n\n<p>Partly because dealing with JSON with curl sometimes make the involved command lines rather long and quirky.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"json-in-curl\">JSON in curl<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/github.com\/curl\/curl\/discussions\/8312\">discussion has been ignited<\/a> in the curl community about what, if anything, we should do in curl to make it a smoother and better tool when working with JSON. The offered opinions range from <strong>nothing<\/strong> (&#8220;curl is content agnostic&#8221;) to <strong>full-fledged<\/strong> JSON generator, parser and pretty-printer (or a combination in between). I don&#8217;t think we are ready to put our collective foot down on where we should go with this.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introducing-json\">Introducing <code>--json<\/code><\/h2>\n\n\n\n<p>While the discussion is ongoing, we still bring a first JSON oriented feature to the tool as a first step. The <code>--json<\/code> option. This is a new option that basically works as an alias, or shortcut, to sending JSON to an endpoint. You use it like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl --json '{\"tool\": \"curl\"}' https:\/\/example.com\/<\/pre>\n\n\n\n<p>Send JSON from a file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl --json @json.txt https:\/\/example.com\/<\/pre>\n\n\n\n<p>Or send JSON passed to curl on stdin:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo '{\"a\":\"b\"}' | curl --json @- https:\/\/example.com\/<\/pre>\n\n\n\n<p>This change does not affect the libcurl library at all, it is only done the tool side of things.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"details\">Details<\/h2>\n\n\n\n<p>The <code>--json<\/code> option is the equivalent of setting<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">--data [arg]<br>--header \"Content-Type: application\/json\"<br>--header \"Accept: application\/json\"<\/pre>\n\n\n\n<p>If you want another Content-Type or Accept header, then you can override them as usual with <a href=\"https:\/\/everything.curl.dev\/http\/requests\/headers\">-H<\/a>. If you use multiple <code>--json<\/code> options on the same command line, the contents from them will be concatenated before sent off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ships\">Ships!<\/h2>\n\n\n\n<p>This new json option was merged in <a href=\"https:\/\/github.com\/curl\/curl\/commit\/32160cae847b4e42d52edb033615f88b12ce5c05\">this commit<\/a> and will be available and present in curl 7.82.0, to be released in early March 2022. Of course you can build it from git or a <a href=\"https:\/\/curl.se\/snapshots\/\">daily snapshot<\/a> already now to test it out!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"future-json-stuff\">Future JSON stuff<\/h2>\n\n\n\n<p>I want to at least experiment a little with some level of JSON creation for curl. To help users who want to create basic JSON on the command line to send off. Lots of users run into problems with this since JSON uses double quotes a lot and then the combination of quoting and unquoting and the shell often make users confused or even frustrated.<\/p>\n\n\n\n<p>Exactly how or to what level is still to be determined. Your opinions and feedback are very valuable and helpful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"jo-curl-jq\">jo+curl+jq<\/h2>\n\n\n\n<p>This trinity of commands helps you send correctly formatted data and show the returned JSON easily. Like in a command line similar to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">jo name=daniel tool=curl | curl --json @- https:\/\/httpbin.org\/post | jq<\/pre>\n\n\n\n<p><a href=\"https:\/\/jpmens.net\/2016\/03\/05\/a-shell-command-to-create-json-jo\/\">jo<\/a>, <a href=\"https:\/\/stedolan.github.io\/jq\/\">jq<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The curl &#8220;cockpit&#8221; is yet again extended with a new command line option: &#8211;json. The 245th command line option. curl is a generic transfer tool for sending and receiving data across networks and it is completely agnostic as to what it transfers or even why. To allow users to craft all sorts of transfers, or &hellip; <a href=\"https:\/\/daniel.haxx.se\/blog\/2022\/02\/02\/curl-dash-dash-json\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">curl dash-dash-json<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":15180,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[33,509],"class_list":["post-18389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-curl","tag-curl-and-libcurl","tag-json"],"_links":{"self":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/18389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/comments?post=18389"}],"version-history":[{"count":18,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/18389\/revisions"}],"predecessor-version":[{"id":18419,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/18389\/revisions\/18419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media\/15180"}],"wp:attachment":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media?parent=18389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/categories?post=18389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/tags?post=18389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}