![]() Just to make this a little cleaner, we can make use of compose, which takes a variadic list of procedures, and composes them together. Now the output of this function is a jsexpr, instead of a string. ![]() ![]() ( require json) ( require net/url) ( call/input-url ( string->url "") get-pure-port ( lambda ( port) ( string->jsexpr ( port->string port)))) Here’s an example of passing a procedure that takes a port and returns a jsexpr, Racket’s representation of a JSON document. Realistically you could pass a procedure that takes a port and returns anything, and that would be the output of call/input-url. Port->string is a procedure that converts a port to a string. An impure port is one that still has its MIME headers.īy passing the procedure get-pure-port to call/input-url, we’re specifying that we do not need the MIME headers. As far as the difference between an pure port and an impure port, here’s a quote from the Racket docs.Ī pure port is one from which the MIME headers have been removed, so that what remains is purely the first content fragment. This is essentially a stream or buffer used with which to fill the response. My initial thought was that it meant a TCP/UDP port, but in Racket, a port is something that produces or consumes bytes. Get-pure-port is a little misleading if you’re not familiar with Racket. Giving string->url a full URL like the one above will return a populated url struct, while (string->url "index.html") will return a url struct with only the path field populated with “index.html”. String->url takes a string, and returns a url struct that contains fields for each segment of the url It’s cool that it works, but string->url, get-pure-port, and port->string could use some more explanation. That snippet will return a string of the JSON representation of The Orange Teardown on. It works like so ( require net/url) ( call/input-url ( string->url "") get-pure-port port->string) The closest thing I found was the call/input-url function from the net/url package. I started this off by looking for a super simple function that takes a URL and returns the body of the response as a string. This first post explains how to do simple HTTP GETs to a JSON REST API, and then parse the JSON response into a form that you can traverse and manipulate in Racket. Some of these uses include getting and posting to a REST API, reading and writing to files, parsing formats like JSON, XML, or CSV, and using a database. The purpose of this series is to document some practical uses of Racket, and explain any rough edges that I run into and some ways to soften those out. I’ve been able to apply some of the functional patterns I’ve learned to other languages like PHP or JavaScript, but I’d also like to be able to use Racket as a tool when writing more general programs. This includes writing a small interpreter in a programming languages class in college (which was super cool) and learning functional programming patterns from a book like The Structure and Interpretation of Computer Programs (SICP). I’ve mostly been discouraged from writing smaller, one-off programs in Scheme/Racket because I’ve only used it in an academic context. Practical Racket: Using a JSON REST API Why Racket?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |