TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The time might come when we add some JSON specific command line options

536 pointsby dennis-traover 3 years ago

48 comments

CobrastanJorjiover 3 years ago
If you regularly do command line JSON requests, I&#x27;m a big fan of HTTPie. It&#x27;s so much easier to use correctly. <a href="https:&#x2F;&#x2F;httpie.io&#x2F;docs&#x2F;cli&#x2F;examples" rel="nofollow">https:&#x2F;&#x2F;httpie.io&#x2F;docs&#x2F;cli&#x2F;examples</a><p>For example, here&#x27;s a JSONy POST request with cURL:<p>curl -s -H &quot;Content-Type: application&#x2F;json&quot; -X POST <a href="https:&#x2F;&#x2F;api.ctl.io&#x2F;v2&#x2F;authentication&#x2F;login" rel="nofollow">https:&#x2F;&#x2F;api.ctl.io&#x2F;v2&#x2F;authentication&#x2F;login</a> --data &#x27;{&quot;username&quot;:&quot;YOUR.USERNAME&quot;,&quot;password&quot;:&quot;YOUR.PASSWORD&quot;}&#x27;<p>Here&#x27;s that same request with HTTPie:<p>http POST <a href="https:&#x2F;&#x2F;api.ctl.io&#x2F;v2&#x2F;authentication&#x2F;login" rel="nofollow">https:&#x2F;&#x2F;api.ctl.io&#x2F;v2&#x2F;authentication&#x2F;login</a> username=YOUR.USERNAME password=YOUR.PASSWORD
评论 #30012617 未加载
评论 #30012962 未加载
评论 #30012581 未加载
评论 #30012714 未加载
评论 #30013260 未加载
评论 #30013188 未加载
评论 #30014594 未加载
评论 #30031386 未加载
评论 #30012608 未加载
评论 #30015769 未加载
评论 #30016628 未加载
评论 #30018457 未加载
评论 #30018346 未加载
hn_throwaway_99over 3 years ago
The --jp (json part) command line option, described at <a href="https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON</a>, has &quot;anti-pattern&quot; written all over it to me. Why introduce some specific, curl-only wonky-ish version of JSON? Is this any easier to remember than normal JSON? I mean, right now, I use cURL all the time with JSON posts, just doing something like<p>-d &#x27;{ &quot;foo&quot;: &quot;bar&quot;, &quot;zed&quot;: &quot;yow&quot; }&#x27;<p>The proposed --jp flag seems worse to me in every way.<p>(Note I do like the --json as just syntactic sugar for -H &quot;Accept: application&#x2F;json&quot; -d &lt;jsonBody&gt;)
评论 #30013801 未加载
评论 #30013605 未加载
评论 #30014581 未加载
评论 #30013622 未加载
评论 #30015061 未加载
评论 #30030720 未加载
pettersover 3 years ago
&gt; --jp a=b --jp c=d --jp e=2 --jp f=false<p>Uh oh, this looks like it would have the problems of yaml. The data type changes based on the provided string.
评论 #30016784 未加载
leifgover 3 years ago
To everyone saying &quot;just use tool x for this&quot;: the advantage of curl is that is so widely available.<p>For your development laptop you can install anything you want but more often than not you need to log into a EC2 instance, a Docker container you name it.<p>Curl is often pre installed or very easy to install. I know it&#x27;s usually not an up to date version but as time goes by you will be able to rely on this feature on pretty much any machine.
评论 #30016208 未加载
评论 #30013714 未加载
kodahover 3 years ago
I was wondering, &quot;Why not pipe output to JQ&quot; up until I read this:<p>&gt; A not insignificant amount of people on stackoverflow etc have problems to send correct JSON with curl and to get the quoting done right, as json uses double-qoutes by itself and shells don&#x27;t expand variables within single quotes etc.<p>It&#x27;s about sanitized inputs.
评论 #30012769 未加载
评论 #30013161 未加载
greennover 3 years ago
Sounds like this idea is limited to the curl tool and wouldn&#x27;t add anything to libcurl, which is great. I&#x27;d prefer libcurl leaving JSON to other libraries.<p>I use bash variables inside JSON with curl all the time, which leads to string escape screw ups. I know there are alternatives that make testing REST + JSON easier, but since our software uses libcurl in production I prefer to test with curl to keep things consistent.
drewdaover 3 years ago
I like using the httpie CLI, in part because it has a nice interface for sending JSON and receiving JSON: <a href="https:&#x2F;&#x2F;httpie.io&#x2F;docs&#x2F;cli&#x2F;json" rel="nofollow">https:&#x2F;&#x2F;httpie.io&#x2F;docs&#x2F;cli&#x2F;json</a>
评论 #30013849 未加载
justin_oaksover 3 years ago
I can see this being useful, but I&#x27;m not looking forward to the list of command line options being even longer. The output of &quot;curl --help&quot; on my system is already 212 lines long.<p>I wish the curl command was split such that different protocols had different commands. I REALLY don&#x27;t want to see a list of FTP specific command line options whenever I&#x27;m just trying to look up a lesser-used HTTP option.<p>That said, this is really a minor gripe compared to just how useful curl has been for me over the years.
评论 #30013423 未加载
评论 #30013196 未加载
评论 #30014904 未加载
hnarnover 3 years ago
In the linked github wiki there&#x27;s an example of the syntax of the suggested --jp flag used to pass key-value pairs and put them together as a JSON object:[1]<p>--jp a=b --jp c=d --jp e=2 --jp f=false<p>Gives:<p>{ &quot;a&quot;: &quot;b&quot;, &quot;c&quot;: &quot;d&quot;, &quot;e&quot;: 2, &quot;f&quot;: false }<p>--jp map=europe --jp prime[]=13 --jp prime[]=17 --jp target[x]=-10 --jp target[y]=32<p>Gives:<p>{ &quot;map&quot;: &quot;europe&quot;, &quot;prime&quot;: [ 13, 17 ], &quot;target&quot;: { &quot;x&quot;: -10, &quot;y&quot;: 32 } }<p>While this is neat, I suppose, it seems like such a waste that the first one isn&#x27;t given as:<p>--jp a=b,c=d,e=2,f=false<p>And the second as:<p>--jp map=europe --jp prime[]=13,17 --jp target[]=x:-10,y:32<p>...or similar. The repetition kind of bothers me.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON</a>
评论 #30014752 未加载
ainar-gover 3 years ago
I feel like if you only want to make a single JSON request, a simple curl invocation with the JSON data in single quotes or in a file should be enough. And if you make many different JSON requests, you&#x27;re probably much better off with one of the alternative tools.<p>Related to the second point, I really wish more people put more time into creating tools for their testers. Shell&#x2F;Ruby&#x2F;Python&#x2F;Perl scripts that are custom-made for the specific service they&#x27;re testing and provides better UI. So that instead of a sequence of curl invocations, logins, and error-prone copy-pasting, people could just:<p><pre><code> test-my-service --user j.doe:hunter2 --api comments&#x2F;create --param body=&quot;hello world&quot;</code></pre>
advisedwangover 3 years ago
Some of the replies say this is a layer violation: HTTP doesn&#x27;t care about JSON so curl shouldn&#x27;t either. But you have to add Content-type and Accept headers when working in JSON, which I personally often forget, so I think this does make sense.
评论 #30013213 未加载
Machaover 3 years ago
I&#x27;m indifferent if they do this or not, can always use pipes and jq, but if they do, I hope the json-part option uses some syntax that&#x27;s a subset of jsonpath and&#x2F;or jq, so I don&#x27;t have to understand a third syntax when people start using this.
softwarebewareover 3 years ago
I think this idea violates the Unix Philosophy. What should happen is that a separate utility could be used to pipe in the request body to cURL similar to <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;12583930&#x2F;use-pipe-for-curl-data" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;12583930&#x2F;use-pipe-for-cu...</a>
评论 #30013895 未加载
评论 #30018406 未加载
评论 #30013920 未加载
评论 #30017122 未加载
评论 #30016898 未加载
评论 #30016875 未加载
er0kover 3 years ago
Check out curlie[0] which is really great and already does this. It&#x27;s essentially a wrapper for curl with JSON support.<p>[ 0 ] <a href="https:&#x2F;&#x2F;github.com&#x2F;rs&#x2F;curlie" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rs&#x2F;curlie</a>
tester756over 3 years ago
Great to see that author is open minded and pragmatic
wmanleyover 3 years ago
It seems the goal is to make it easier to craft JSON by having curl perform escaping, while the proposal would seem to require some sort of in-memory tree representation of the data.<p>One alternative would be to provide escaping more directly like this:<p><pre><code> curl --json &#x27;{ &quot;map&quot;: %s, &quot;prime&quot;: [ %i, %i ], &quot;target&quot;: { &quot;x&quot;: %i, &quot;y&quot;: %i } }&#x27; &quot;$continent&quot; &quot;$p1&quot; &quot;$p2&quot; &quot;$x&quot; &quot;$y&quot; https:&#x2F;&#x2F;example.com </code></pre> And then curl would do the substitution with the appropriate type-specific escaping for each variable. This has a few nice properties:<p>1. What&#x27;s on the command line resembles what&#x27;s actually going to be sent.<p>2. Curl doesn&#x27;t actually need to parse (nor validate) the JSON, or to create a tree representation of the data within itself. %s is invalid JSON anyway, so you can do a string substitution - all you need to keep track of are matching quotes (including escape sequences).<p>I&#x27;ve used a printf style format string here, which could be expanded for extra convenience. For example the Python-style `%(env_var)s` sequences could be used which could expand environment variables directly. Or something could be added for convenient handling of bash arrays.
gumbyover 3 years ago
JSON is underspecified, leading to various incompatibilities between implementations.<p>Because cURL is so ubiquitous, whatever Daniel implements may become the de facto standard.
评论 #30012930 未加载
评论 #30012560 未加载
评论 #30014657 未加载
jdc0589over 3 years ago
This <i>would</i> provide some additional utility, but honestly I don&#x27;t see the point. Anyone sending JSON via curl CLI a lot is probably having to manipulate JSON via CLI for purposes <i>other</i> than sending requests with curl as well. It makes more sense for most people to just learn one json manipulation tool and pipe input in and out of things that need it.
stackedinserterover 3 years ago
Also, setting &quot;content-type: application&#x2F;json&quot; in POST&#x2F;PUT&#x2F;PATCH requests would be helpful.
评论 #30014822 未加载
abotsisover 3 years ago
What happened to “Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new &quot;features&quot;.”?<p>If it’s too tough to integrate with other tools like jq, maybe that could provide for a better outcome.
评论 #30014609 未加载
评论 #30015368 未加载
adolphover 3 years ago
I use cURL a lot. I can see how this would maybe somewhat useful for working very quickly, but the wiki-given use cases of k-v pairs and lists are simple enough in raw JSON.<p>Something that would be helpful is for cURL, HTTPie, Postman, Fiddler, etc to standardize on a request&#x2F;response pair format such as Chrome&#x27;s HAR. There are some tools in NPM and the below HAR to cURL too, so I think native HAR support would be more helpful than a JSON builder.<p><a href="https:&#x2F;&#x2F;mattcg.github.io&#x2F;har-to-curl&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mattcg.github.io&#x2F;har-to-curl&#x2F;</a>
mgover 3 years ago
I would rather write a new tool - say jcurl - which uses curl under the hood.<p>As a user I would not expect curl to have json functionality.<p>And as a developer I would prefer to have one codebase deal with http and another one with json.
评论 #30012877 未加载
评论 #30013961 未加载
JRGC1over 3 years ago
Perhaps use an existing JSON command line tool and piping it into Curl?
评论 #30012934 未加载
throwaddzuzxdover 3 years ago
I&#x27;ve included --json in a custom redefinition for years, glad to see something like that coming to the official binary!<p><pre><code> curl() { args=() for arg in &quot;$@&quot;; do case $arg in --json) args+=(&quot;-H&quot; &quot;Content-Type: application&#x2F;json&quot;) ;; *) args+=(&quot;$arg&quot;) ;; esac done command curl &quot;${args[@]}&quot; }</code></pre>
fraover 3 years ago
If this means I can just use libcurl to GET a web endpoint and parse the JSON in a C program rather than have to manage multiple dependencies, I&#x27;m all for it!
smrtinsertover 3 years ago
Please no, I&#x27;m thinking of me having to deal with someones 800 line curl script in the future. Agreed this doesn&#x27;t feel very unixy.
评论 #30014870 未加载
otarover 3 years ago
Hmm… any actual use cases of this? I don’t find curl —jp a=b to be better than directly sending a payload on a HTTP resource.
评论 #30012921 未加载
评论 #30012858 未加载
pbiggarover 3 years ago
This is great. When a new user uses Darklang, we want them to be able to make JSON API requests quickly and easily, and there aren&#x27;t great client-side tools for that that you can expect users to have installed. giving them a big long curl command is no fun, but `curl --json &#x27;the-body&#x27; would be amazing`
syspecover 3 years ago
Doesn&#x27;t really look like it&#x27;s adding anything, and the `jp` part looks like the people referenced on stackoverflow will just be more confused.<p>Often times the JSON being sent down is complex, I can&#x27;t imagine anyone wanting to basically rewrite it into something else for anything other than 2 field JSON objects
softwarebewareover 3 years ago
I feel like jq already cornered the market on this. I&#x27;m unlikely to go back and update my scripts to use curl
jeremyjhover 3 years ago
I know I&#x27;ve done the quoting dance before, while exploring an API in one project I resorted to using zsh heredocs to build the payload argument to avoid all quoting issues. I&#x27;m sure there is a better way already but it sounds nice to have this built into curl as its so common.
bonkabonkaover 3 years ago
I would prefer to use the --json flag to provide syntactic sugar for setting the content type and accepts headers and leave the marshaling of data to a separate tool. Or if it has to be baked in, refactor `jo` into `libjo` and a CLI wrapper so that the two tools behave the same way.
svnpennover 3 years ago
Shouldn&#x27;t this be a GitHub issue or GitHub discussion? Wiki is a weird format to use for a proposal.
评论 #30014194 未加载
Zamicolover 3 years ago
I use cURL for local development with JSON cookies and I think it&#x27;s perfectly adequate for that purpose.<p>curl --insecure --cookie test_cookie=&#x27;{&quot;test&quot;:&quot;bob&quot;}&#x27; <a href="https:&#x2F;&#x2F;localhost:8081&#x2F;" rel="nofollow">https:&#x2F;&#x2F;localhost:8081&#x2F;</a>
评论 #30012682 未加载
评论 #30012659 未加载
评论 #30012803 未加载
svnpennover 3 years ago
Shouldn&#x27;t this be a GitHub issue or GitHub discussion:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;curl&#x2F;curl&#x2F;wiki&#x2F;JSON</a><p>Wiki is a weird format to use for a proposal.
评论 #30013254 未加载
pkruminsover 3 years ago
So great! This has been one of the most requested curl features for years. Without this feature, to send JSON, you had to craft a valid JSON string yourself or shell out to another utility that creates a valid JSON string.
plucover 3 years ago
<a href="https:&#x2F;&#x2F;curl.se&#x2F;mail&#x2F;archive-2022-01&#x2F;0043.html" rel="nofollow">https:&#x2F;&#x2F;curl.se&#x2F;mail&#x2F;archive-2022-01&#x2F;0043.html</a>
datavirtueover 3 years ago
I just want to pass it a filename that contains the JSON. Never been a fan of heaving around post bodies that dangle from a curl command...and I hate postman.
tastroderover 3 years ago
dupe of <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30011382" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30011382</a>
评论 #30013029 未加载
pixel_tracingover 3 years ago
How about opening up an Vim or Nano like editor with an option flag —editor where I can just paste the request body instead of passing flags, etc.?
intrasightover 3 years ago
I now use a C# REPL like csharp-script rather than such task specific scripts such as curl. It&#x27;s more flexible, powerful, and consistent.
999900000999over 3 years ago
On one hand, this is awesome.<p>But aren&#x27;t there also several command line utilities which already support JSON.<p>Why cram new stuff into such an industry standard tool?
评论 #30012902 未加载
评论 #30012770 未加载
评论 #30012804 未加载
评论 #30012811 未加载
willciprianoover 3 years ago
I imagine this will be part of libcurl? If so that makes it a one stop shop for JSON REST programming in C.
bborudover 3 years ago
I wish curl had CoAP support
axiosgunnarover 3 years ago
Btw, did you notice how quickly the page loaded?
a45a33sover 3 years ago
will libcurl have built in json support or just the command line?
chrismellerover 3 years ago
Thank $deity. Jq suuuucks.
评论 #30013234 未加载
blibbleover 3 years ago
hopefully we&#x27;ll be getting command line arguments to build up XML documents soon too.....?<p>(not serious)
评论 #30014903 未加载