Hacker News new | ask | show | jobs
by ctoth 1036 days ago
Last time I looked at this I couldn't really find an easy standard way to make HTTPS API calls and parse JSON, but only spent an hour or two playing with it. Can you point me at the canonical way to do this? What's the Requests of FreePascal?
4 comments

Fun, I struggled with json parsing for a long time, and have done it for the first time with freepascal. Maybe this can help you : https://medium.com/@marcusfernstrm/freepascal-and-json-337c0... ?
That is way easier than the stuff I was reading before which said I needed some sort of 3rd party paid component for it, nice.
Third party paid component for parsing strings into basic data types and making http requests? That doesn't sound right even for late 20th century
I love comments like this because it shows me how diverse this field is, and how different everyone's experience and expectations are.

As a counterpoint to your comment, I make a living writing high-quality libraries. Everything I make is "in the box", but its not hard to improve on that.

My most popular tools are around string functions, json and xml readers/writers and network functionality.

So, on one hand I'm selling ice-cream in Alaska, on the other hand, it's good ice-cream.

It's a small market, a pimple on a niche, but its big enough to keep me going and that's big enough for me.

Free Pascal has fcl-web which should provide a HTTP/HTTPS client (among other things, it can be used to make web apps), though it comes with libcurl bindings out of the box so you could also use that too. fcl-json provides a JSON parser. FWIW "fcl" means "Free Component Library" which contains various tools (mainly exposed as classes).

The packages[0] directory of FPC contains a lot of stuff that come out of the box with the compiler and they often have an "examples" and/or "tests" directory with code you can check. There is some documentation[1] but sadly FCL is really not that well documented - you just have to check the sources for most things.

Note that this is for Free Pascal itself. Lazarus builds on it (LCL, the "Lazarus Component Library", is built on top of FCL) and adds a bunch of additional components of its own. Though usually for non-GUI stuff you just use the Free Pascal classes, Lazarus has some of its own "wrappers" that integrate with the IDE and the form/object designer. The "weblaz" package (it comes with Lazarus but you need to install it manually from Package -> Install/Uninstall Packages) provides a bunch of components for working with the web (mainly for making web apps), including the "TFPHttpClient" component which can be used to make HTTP requests. As a simple example, if you throw a TMemo (multiline text editor) control in a form, throw a TFPHttpClient component and then doubleclick on the form to edit the code to execute during the form's creation you can type "Memo1.Text:=FPHTTPClient1.Get('https://news.ycombinator.com/');" and it will put the HTML code for this forum in the memo (note that you may also need to add the opensslsockets unit in the uses section at the top of the code). Of course that is a very simple example but if you browse the properties and events of the component in the object inspector as well as the available methods by typing "FPHTTPClient1." and pressing ctrl+space in the code editor you can find most of the other functionality the component provides.

[0] https://gitlab.com/freepascal.org/fpc/source/-/tree/main/pac...

[1] https://www.freepascal.org/docs-html/current/fcl/index.html

For Delphi it is a combo of the Indy project (IdHttp), and superobject (probably other json parsers too but superobject is quick and fairly solid).

However, given I've yet to find Delphi code that straight up compiles in Lazarus/FreePascal, you probably have your work cut out for you, but if you google for [superobject/indy] for Lazarus, others have started those hikes, and you may find there are working versions of one, or possibly both of them. I've never looked.

Does superobject compile under Lazarus?