首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >持枪http客户端不打印响应体

持枪http客户端不打印响应体
EN

Stack Overflow用户
提问于 2019-10-16 19:20:57
回答 1查看 166关注 0票数 2

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

代码语言:javascript
复制
(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))))

请求日志是

代码语言:javascript
复制
http cast get state: #{host => <<"httpstat.us">>,path => <<"/200">>,
                       scheme => <<"https">>}
http get #{host => <<"httpstat.us">>,path => <<"/200">>,scheme => <<"https">>}
gun:open msg {gun_up,<0.146.0>,http}
handle get request path: <<"/200">>
get response body fin  <--- how to get the response body content?
status 200
headers [{<<"cache-control">>,<<"private">>},
         {<<"server">>,<<"Microsoft-IIS/10.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:443">>},
         {<<"date">>,<<"Wed, 16 Oct 2019 19:07:29 GMT">>},
         {<<"content-length">>,<<"0">>}]

我能够得到其他urls的响应体。例如:

代码语言:javascript
复制
handle-post <<"http://date.jsontest.com/">>
dreamnet-svc-sup start child [#{url => <<"http://date.jsontest.com/">>}]
http cast get state: #{host => <<"date.jsontest.com">>,path => <<"/">>,
                       scheme => <<"http">>}
http get #{host => <<"date.jsontest.com">>,path => <<"/">>,
           scheme => <<"http">>}
gun:open msg {gun_up,<0.188.0>,http}
handle get request path: <<"/">>
get response body nofin status 200
handle-get-response
handle get response finished <<"{\n   \"date\": \"10-17-2019\",\n   \"milliseconds_since_epoch\": 1571333596738,\n   \"time\": \"05:33:16 PM\"\n}\n">>
lfe>                 

进一步研究这一点,也使用erlang的httpc客户机,主体是空的。

代码语言:javascript
复制
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获得响应。

代码语言:javascript
复制
curl -X GET "https://httpstat.us/200"
200 OK%
EN

回答 1

Stack Overflow用户

发布于 2019-10-17 17:36:41

如果希望同步获得响应,那么在调用gun:get之后,可以使用返回的stream-ref调用gun:await。请看枪:等待的枪支文档。所以,就像:

代码语言:javascript
复制
(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消息为止。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58420350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档