Replies: 12 comments 21 replies
-
I saw that JSON display is handled by piping to |
Beta Was this translation helpful? Give feedback.
-
Evolving a bit from your ideas- maybe combining bracket annotations to create lists versus
Since these are probably the same
|
Beta Was this translation helpful? Give feedback.
-
Initial trial balloon for |
Beta Was this translation helpful? Give feedback.
-
I have a concern about data types with In the example
My concern is that there doesn't seem to be a way to specify what type you want, what would I do if i wanted to send I suggest adding an optional way to specify types, here's an example of how I see it:
|
Beta Was this translation helpful? Give feedback.
-
I'd argue that as you prefer to delegate output parsing to jq, you could prefer to delegate input building to jo (or something similar). Maybe the automatic configuration of Another problem I see with letting the curl monolith entirely build the json body is that it's not composable.
|
Beta Was this translation helpful? Give feedback.
-
After first having raid some of the mailing post messages, I was like, what are you guys on about, of course is having a json cmdline great. It's not that different from what the other encoding options do, right? I hadn't yet looked at the proposal. As a user of cURL to do API requests mostly, I also ran into inconveniences when posting JSON data. Now forgive me if there IS a way to properly escape the JSON payload and I was just ignorant ;) The issue I ran into initially was escaping new lines 0, but lets also add a brief summary first. When doing REST API calls, we suffer two issues really, one that's caused by the shell, the other by the content. The shell we can do little about, it will consume quotes in certain forms. For completeness sake, I'm speaking of $ curl ... {"key": "value"} won't work, because the shell will eat the quotes and you end up with invalid JSON, and so obviously a cheap trick is to quote the whole thing (which very often people will do anyway. $ curl ... '{"key": "value"}' This then of course breaks again once you want to do actually some fun stuff, like including variables that the shell needs to expand, and so you end up with having to escape the double quotes which are required as part of the JSON format (and so it might be argued that it is a more accurate representation :p). This is just shell shenanigans that cURL can't do much about anyway, other then 'deciding' to properly quote things in the payload, which makes this feature explode from MVP complexity. $ curl ... "{\"key\": \"${value}\"}" Now to the beefy parts. JSON actually doesn't allow you to send various characters, and they need to properly be escaped. My feeling is, this is what most people complain about when talking about 'having to escape JSON'. When looking at a decently common JSON implementation, we can also see this clear as day 1 what in addition needs to be escaped. If a common JSON parser does this, would it not make sense that also cURL does this (Again, if there is a different way to escape exactly this set of characters in a different way, ignore my ignorance and delete my post :p)? I'm sure the above can easily be solved using jq and what not, but I'm a CI user, and not wanting to have to build or use random containers, when ALL I want to do is do a few REST requests, I prefer to use the official cURL Docker container and so I end up using shenanigans like 2. To get back to the original proposal, I think that including the proper content type by default behind Further more, another upcomming hot potato is graphQL posting over REST. While still 100% JSON, it is a bit ugly, in that it looks like: $ curl ... --json '{"query": "<graphql content>"}' One could argue though, that once the popularity of graphql increases and json parsing already exists within cURL, this could easily be wrapped internally, so that $ curl ... --quaphql '<graphql content>' gets turned into the earlier internally (BUT I'm not certain if the keyname (query) is a GitLab specific thing or a graphql library specific thing or a graphql spec thing). Sorry for the very long post @bagder, but I hope this help clarify some problems and thoughts? P.S. I do fully agree this should only be about input. If a consumer actually needs to process the output, for that, there are better ways as things become very complex very quickly to support that and you'd end up just building a mini |
Beta Was this translation helpful? Give feedback.
-
I like the idea of Seconding @dch for a convenience flag to set both headers. I'm not sure |
Beta Was this translation helpful? Give feedback.
-
`Accept: application/json, */*` is what is needed.
This is fine for browsers. It should not be the default for a command line tool that expects to parse the response reliably.
Are there actual APIs that people use with curl where this is necessary? I’ve not encountered one where this would be a useful fallback. Or does this come up in pretending to be a browser, maybe in testing?
|
Beta Was this translation helpful? Give feedback.
-
In July I made this project, I can relicense under CC0 if you want… maybe (only question is that its mostly based on your libcurl examples): https://github.com/SamuelMarks/curl-simple-https In addition to simplified struct ServerResponse
https_json_post(CURLU *urlp, const char *body, struct curl_slist *headers) {
return https_wrapper(urlp, make_request_post, body, set_json_headers(headers));
} Where that return type is defined as: struct ServerResponse {
CURLcode code;
/* What else would be good? */
struct curl_slist *headers;
long status_code;
const char *body;
}; My code is nothing special, so feel free to just think of this as a concept suggestion; though I can send through a PR if you would be interested. Can I encourage you to have simple Thanks for your consideration PS: the big issue with my codebase, apart from lack of docs and tests [!!], is that I don't have any principles stance around allocations. Like calling |
Beta Was this translation helpful? Give feedback.
-
Hi there, {
"name":"max",
"age":30,
"kids":[
"kirk",
"john"
],
"car":{
"name":"speedo",
"speed":200,
"electric":false
}
} could be written in object based notation:
or in key based notation:
Which is especially useful if your JSON structure is deep without many values. If anyone is interested here is a link to the (Apache 2 licensed) Java project and here is one to the actual parser that does all the magic. Maybe this is inspiring/helpful in any way :) |
Beta Was this translation helpful? Give feedback.
-
Isn't urlon just a convention for structuring URLs? curl supports URLs just
fine (it's even in the name).
|
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
If you feel you want to comment on or contribute to the brainstorm around json support. Here's your chance!
Beta Was this translation helpful? Give feedback.
All reactions