在Racket中发送HTTP POST [英] Sending HTTP POST in Racket

查看:131
本文介绍了在Racket中发送HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过网络中的http / post发送一个字符串,这是我在阅读 Racket HTTP客户端文档

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.

推荐答案

In例如,主机名只是 www.w3.org 部分 - 不包括方案(http或https),也不包括任何路径。例如,这确实有效:

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)

在Racket中,函数可以返回多个值。 http-sendrecv 返回3, define-values 将每个值分配给变量。

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 提供其他功能,让您与主机建立连接,在该连接上发出多个请求,然后关闭连接。

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.

这篇关于在Racket中发送HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆