Erlang Gun http客户端未打印响应体

Erlang Gun http客户端未打印响应体,erlang,lfe,Erlang,Lfe,我有一个http gen_服务器,它使用gun作为http客户端库。我能够打开连接并发出GET请求。但是,我没有收到响应正文,但收到的消息是'fin。如何在异步模式下获得gun响应 (defun get (url-map) (io:format "http get ~p~n" `(,url-map)) (let* ((`#(ok ,con-pid) (gun:open (binary:bin_to_list (map-get url-map 'host))

我有一个http gen_服务器,它使用gun作为http客户端库。我能够打开连接并发出GET请求。但是,我没有收到响应正文,但收到的消息是
'fin
。如何在异步模式下获得gun响应

(defun get (url-map)
  (io:format "http get ~p~n" `(,url-map))
  (let* ((`#(ok ,con-pid) (gun:open (binary:bin_to_list (map-get url-map 'host))
                                    (if (== (map-get url-map 'scheme) #"https")
                                      443
                                      80)))
         (mon-ref (monitor 'process con-pid)))
    (receive
      (msg
       (progn
         (io:format "gun:open msg ~p~n" `(,msg))
         (handle-get-request url-map con-pid mon-ref)))
     ((after 3000) (exit 'timeout)))))

(defun handle-get-request (url-map con-pid mon-ref)
  (io:format "handle get request path: ~p~n" `(,(map-get url-map 'path))) 
  (let ((stream-ref (gun:get con-pid (map-get url-map 'path))))
    (receive
      ((tuple 'gun_response con-pid stream-ref 'fin status headers)
       (progn
         (io:format "get response body fin~nstatus ~p~nheaders ~p~n"
                    `(,status ,headers))
         'no_data))  ; <--- how to get the response body content?
      ((tuple 'gun_response con-pid stream-ref 'nofin status headers)
       (progn
         (io:format "get response body nofin status ~p~n" `(,status))
         (handle-get-response url-map con-pid mon-ref stream-ref)))
      ((tuple 'DOWN mon-ref 'process con-pid reason)
       (io:format "handle get request error ~p~n" `(,reason))
       (exit reason))
      ((after 5000) (exit 'timeout)))))


(defun handle-get-response (url-map con-pid mon-ref stream-ref)
  (io:format "handle-get-response~n")
  (receive
    ((tuple 'gun_data con-pid stream-ref 'nofin data)
     (progn
       (io:format "get response data ~p~n" `(,data))
       (handle-get-response url-map con-pid mon-ref stream-ref)))
    ((tuple 'gun_data con-pid stream-ref 'fin data)
     (io:format "handle get response finished ~p~n" `(,data)))
    ((tuple 'DOWN mon-ref 'process con-pid reason)
     (io:format "handle get request error ~p~n" `(,reason))
     (exit reason))
    ((after 5000) (exit 'timeout))))
可能是服务器有问题,但我可以使用curl获得响应

curl -X GET "https://httpstat.us/200"
200 OK%

如果希望同步获取响应,则在调用
gun:get
后,可以使用返回的
stream ref
调用
gun:wait
。有关详细信息,请参阅枪支文档。比如说:

(let (stream-ref (gun:get con-pid (map-get url-map 'path))))
(receive
((tuple 'data 'fin data)
 (progn
   (io:format "get response data ~p~n" `(,data))

但是,您不想等待响应,因此您可以尝试在原始接收上循环,直到获得所需的消息类型。您可以将原始接收放在递归函数中,然后在获得所需消息类型时结束递归函数。换句话说,您仍然会收到
'fin
消息,但您的函数不会到此结束——它会一直接收消息,直到收到
'data
消息。

但这会导致调用阻塞,我正在寻找一个异步解决方案。@jsloop,gen_服务器的
handle_cast()
是异步的。
lfe> (lists:map (lambda (a) (application:start a)) '(crypto asn1 public_key ssl inets))
lfe> (httpc:request 'get `#("https://httpstat.us/200" ()) '() '())
#(ok
  #(#("HTTP/1.1" 200 "OK")
    (#("cache-control" "private")
     #("date" "Thu, 17 Oct 2019 17:29:50 GMT")
     #("server" "Microsoft-IIS/10.0")
     #("content-length" "0")
     #("x-aspnetmvc-version" "5.1")
     #("access-control-allow-origin" "*")
     #("x-aspnet-version" "4.0.30319")
     #("x-powered-by" "ASP.NET")
     #("set-cookie"
       "ARRAffinity=93fdbab9d364704de8ef77182b4d13811344b7dd1ec45d3a9682bbd6fa154ead;Path=/;HttpOnly;Domain=httpstat.us"))
    ()))
curl -X GET "https://httpstat.us/200"
200 OK%
(let (stream-ref (gun:get con-pid (map-get url-map 'path))))
(receive
((tuple 'data 'fin data)
 (progn
   (io:format "get response data ~p~n" `(,data))