Sending HTTP POST in Racket

被刻印的时光 ゝ 提交于 2019-12-09 18:31:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31236374/sending-http-post-in-racket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!