Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP POST in Racket

I am trying to send a string via http/post in Racket, this is what I tried so far after reading the Racket HTTP Client Documentation

#lang racket

(require net/http-client)

(define
  myUrl "https://something.com")

(http-conn-send!
   (http-conn-open
    myUrl
    #:ssl? #t)
   #:version "1.1"
   #:method "POST"
   #:data "Hello")

But with this I receive the following error:

tcp-connect: connection failed
  detail: host not found
  address: https://www.w3.org/
  port number: 443
  step: 1
  system error: nodename nor servname provided, or not known; errno=8

I tried it with several different adresses.

I am new to racket and programming in general and unable to figure out what I am missing.

like image 309
Jessica Nowak Avatar asked Feb 09 '23 16:02

Jessica Nowak


1 Answers

In your example, the hostname is only the www.w3.org portion -- not including the scheme (http or https) nor any path. So for example this does work:

(http-conn-open "www.w3.com"
                #:ssl? #t)

To make a post request, you could do this:

#lang racket

(require net/http-client)

(define-values (status headers in)
  (http-sendrecv "www.w3.com"
                 "/"
                 #:ssl? #t
                 #:version "1.1"
                 #:method "POST"
                 #:data "Hello"))
(displayln status)
(displayln headers)
(displayln (port->string in))
(close-input-port in)

In Racket, a function can return multiple values. http-sendrecv returns three, and the define-values assigns each one to a variable.

net/http-client provides other functions to let you make a connection to a host, make multiple requests on that connection, then close the connection.

like image 66
Greg Hendershott Avatar answered Feb 24 '23 20:02

Greg Hendershott