Arc Forumnew | comments | leaders | submitlogin
Http requests in arc?
3 points by alexpogosyan 4830 days ago | 6 comments
Is there any way to make http requests in arc? I need it to make API calls to amazon's web services.

Thanks.



4 points by zck 4829 days ago | link

You'll have to hack ac.scm to add a new function to arc. Racket's tcp-connect is the useful function here. I'm not at a computer where I've done this, but it's a matter of adding something like this:

    (require racket/tcp)
    
    ;; xdef takes its second arg, which is Scheme code,
    ;; and binds itto the name given as the first arg.
    ;; The name is accessible only from Arc code, I think.
    (xdef tcp-connect (lambda (hostname port)
                          (let-values (((from-server to-server)
                                        (tcp-connect hostname port)))
                            (list from-server to-server))))
    
Then you use it as follows, in Arc code:

    (let (from-server to-server) (tcp-connect "google.com" 80)
         (disp "GET / HTTP/1.1\nHost: google.com\n\n" to-server)
                 ;; I believe 'disp is the correct function
                 ;; This, I believe, is a correct HTTP request. It works, at least.
         (close to-server) ;; you won't be able to know when the response is done
                           ;; unless you close the input port.
         (whilet line (readline from-server)
                 (prn line)) ;; or whatever you want to do with the results.
         (close from-server) ;; it'll work without it, but close your ports anyway.
Again, this is untested code; parts of it are from memory, and parts are from a previous comment: http://arclanguage.org/item?id=14817 . I'll check this when I get home; tryarc.org is down.

-----

1 point by alexpogosyan 4829 days ago | link

It worked, thank you so much.

I only had to remove (require racket/tcp) line because it was causing 'collection not found' error.

-----

1 point by zck 4829 days ago | link

You're welcome! Weird, though -- I thought the 'require line had to be there so that racket would know what 'tcp-connect is. Apparently I'm wrong; I'll have to try my code without it.

Good luck with the rest of it, though -- this doesn't provide any sort of http header creation, which is somewhat annoying, but if you write some functions for it, I'm sure we'd be interested in seeing them.

-----

1 point by alexpogosyan 4829 days ago | link

Maybe it's because I'm using mzscheme 4.2.5 and not racket?

-----

1 point by zck 4829 days ago | link

Ah, I didn't actually need it either -- I was thinking of some other code I was writing. To call Racket's 'HMAC-SHA1, you need:

  (require web-server/stuffers/hmac-sha1)
  (require net/base64)
But not for tcp-connect. If you make a library for Amazon interaction, that might be useful to other people. I'd be interested in seeing the code, at least.

-----

1 point by typhon 4829 days ago | link

I needed to add this to ac.scm :

  (xdef tcp-connect
        (lambda (host port . rest)
          (call-with-values
            (lambda () (apply tcp-connect host port rest))
            (lambda (x y)
              (cons x (cons y 'nil))))))
This allowed me to write a basic HTTP client.

-----