How to get html response from url string? (Scheme / Racket)

How can I get html response from url string? Using this:

#lang racket (require net/url) (require net/websocket/client) (define google (string->url "http://google.com")) (ws-connect(struct-copy url google [scheme "ws"])) 

Gives me ws-connect: Invalid server handshake response. Expected #"\242\266\336\364\360\"\21~Y\347w\21L\2326\"", got #"<!DOCTYPE html>\n" ws-connect: Invalid server handshake response. Expected #"\242\266\336\364\360\"\21~Y\347w\21L\2326\"", got #"<!DOCTYPE html>\n"

+7
source share
1 answer

I assume that you just want to make the HTTP GET equivalent.

 (require net/url) (define google (string->url "http://google.com")) 

Use get-pure-port to perform an HTTP GET; it returns the input port. In addition, the URL above is redirected, so we must include the following redirects.

 (define in (get-pure-port google #:redirections 5)) 

If you need a single line response, you can use port->string :

 (define response-string (port->string in)) (close-input-port in) 

Or you can pass it to some function that parses it as HTML or XML. There are several such libraries on PLaneT ; I recommend (planet neil / html-parsing: 1) .

See also call/input-url , which automatically handles port closures.

+11
source

All Articles