{"id":13740,"date":"2020-03-17T16:17:01","date_gmt":"2020-03-17T15:17:01","guid":{"rendered":"https:\/\/daniel.haxx.se\/blog\/?p=13740"},"modified":"2020-03-22T23:15:57","modified_gmt":"2020-03-22T22:15:57","slug":"curl-write-out-json","status":"publish","type":"post","link":"https:\/\/daniel.haxx.se\/blog\/2020\/03\/17\/curl-write-out-json\/","title":{"rendered":"curl write-out JSON"},"content":{"rendered":"\n<p>This is not a <a href=\"https:\/\/daniel.haxx.se\/blog\/2020\/01\/07\/curl-option-of-the-week\/\">command line option of the week<\/a> post, but I feel a need to tell you a little about our brand new addition!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><code>--write-out [format]<\/code><\/h2>\n\n\n\n<p>This option takes a format string in which there are a number of different &#8220;variables&#8221; available that let&#8217;s a user output information from the previous transfer. For example, you can get the HTTP response code from a transfer like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl -w 'code: %{response_code}' https:\/\/example.org -o saved<\/pre>\n\n\n\n<p>There are currently 34 different such <a href=\"https:\/\/curl.haxx.se\/docs\/manpage.html#-w\">variables listed and described in the man page<\/a>. The most recently added one is for JSON output and it works like this:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><code>%{json}<\/code><\/h2>\n\n\n\n<p>It is a single variable that outputs a full json object. You would for example invoke it like this when you get data from example.com:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">curl --write-out '%{json}' https:\/\/example.com -o saved<\/pre>\n\n\n\n<p>That command line will spew some 800 bytes to the terminal and it won&#8217;t be very human readable. You will rather take care of that output with some kind of script\/program, or if you want an eye pleasing version you can pipe it into <a href=\"https:\/\/stedolan.github.io\/jq\/\">jq<\/a> and then it can look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  \"url_effective\": \"https:\/\/example.com\/\",\n  \"http_code\": 200,\n  \"response_code\": 200,\n  \"http_connect\": 0,\n  \"time_total\": 0.44054,\n  \"time_namelookup\": 0.001067,\n  \"time_connect\": 0.11162,\n  \"time_appconnect\": 0.336415,\n  \"time_pretransfer\": 0.336568,\n  \"time_starttransfer\": 0.440361,\n  \"size_header\": 347,\n  \"size_request\": 77,\n  \"size_download\": 1256,\n  \"size_upload\": 0,\n  \"speed_download\": 0.002854,\n  \"speed_upload\": 0,\n  \"content_type\": \"text\/html; charset=UTF-8\",\n  \"num_connects\": 1,\n  \"time_redirect\": 0,\n  \"num_redirects\": 0,\n  \"ssl_verify_result\": 0,\n  \"proxy_ssl_verify_result\": 0,\n  \"filename_effective\": \"saved\",\n  \"remote_ip\": \"93.184.216.34\",\n  \"remote_port\": 443,\n  \"local_ip\": \"192.168.0.1\",\n  \"local_port\": 44832,\n  \"http_version\": \"2\",\n  \"scheme\": \"HTTPS\",\n  \"curl_version\": \"libcurl\/7.69.2 GnuTLS\/3.6.12 zlib\/1.2.11 brotli\/1.0.7 c-ares\/1.15.0 libidn2\/2.3.0 libpsl\/0.21.0 (+libidn2\/2.3.0) nghttp2\/1.40.0 librtmp\/2.3\"\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The JSON object<\/h2>\n\n\n\n<p>It always outputs the entire object and the object may of course differ over time, as I expect that we might add more fields into it in the future.<\/p>\n\n\n\n<p>The names are the same as the write-out variables, so you can read the <code>--write-out<\/code> section in the man page to learn more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ships?<\/h2>\n\n\n\n<p>The feature landed in <a href=\"https:\/\/github.com\/curl\/curl\/commit\/04c03416e68fd635a15cae8201872f5c29fdcca8\">this commit<\/a>. This new functionality will debut in the next pending release, likely to be called 7.70.0, scheduled to happen on April 29, 2020.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Credits<\/h2>\n\n\n\n<p>This is the result of fine coding work by Mathias Gumz.<\/p>\n\n\n\n<p>Top image by <a href=\"https:\/\/pixabay.com\/users\/StartupStockPhotos-690514\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=593313\">StartupStockPhotos<\/a> from <a href=\"https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=593313\">Pixabay<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is not a command line option of the week post, but I feel a need to tell you a little about our brand new addition! &#8211;write-out [format] This option takes a format string in which there are a number of different &#8220;variables&#8221; available that let&#8217;s a user output information from the previous transfer. For &hellip; <a href=\"https:\/\/daniel.haxx.se\/blog\/2020\/03\/17\/curl-write-out-json\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">curl write-out JSON<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":13747,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[214,33],"class_list":["post-13740","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-curl","tag-command-line","tag-curl-and-libcurl"],"_links":{"self":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/13740","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=13740"}],"version-history":[{"count":10,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/13740\/revisions"}],"predecessor-version":[{"id":13758,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/13740\/revisions\/13758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media\/13747"}],"wp:attachment":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media?parent=13740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/categories?post=13740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/tags?post=13740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}