3 ms·
>"how much work do I need to put in to get a very simple JSON file from a web URL" (nothing fancy like POST, just an HTTP GET). With python, a call to urllib.re
by DeathArrow 4y ago
>"how much work do I need to put in to get a very simple JSON file from a web URL" (nothing fancy like POST, just an HTTP GET). With python, a call to urllib.request.urlretrieve and then a call json.loads are all you need.
In C# you just have to do this:
var things = await httpClient.GetFromJsonAsync<List<Thing>>("url");
- dekhn 4y agowhy would an http client know anything about json? that's a bad code smell.
- noirscape 4y agoMostly because JSON is one of the most common formats used when sending over data. I think this practice started with pythons requests (which is my real answer as to what you should use in python but I wanted to focus on the stdlib), which has a json function on the Response object for convenience. Most languages nowadays tend to implement some variation of this specific convenience because it's just one of the most frequently needed things; setting up a separate parser and then calling it might be the "cleaner" option, but it's also more boilerplate and the industry has largely moved to try and avoid that.
- Merad 4y agoIt doesn't. It's a standalone (static) helper method that uses HttpClient to perform the request and feeds the response body into a json parser. It's an extension method [0] which means that there's syntactic sugar so that you can write client.GetFromJsonAsync() and the compiler transforms it into the actual static method call, HttpClientJsonExtensions.GetFromJsonAsync(client). 0: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods https://learn.microsoft.com/en-us/dotnet/csharp/programming-...