【发布时间】:2018-08-14 11:37:30
【问题描述】:
我想使用:httpc 作为我的 HTTP 客户端。
我使用 edeliver 进行发布管理等。我的.deliver/config.exs 中有:inets 和:httpc,例如:
set applications: [
:runtime_tools,
:inets,
:httpc
]
我还在mix.exs 中添加了:inets 到:extra_applications。
这是我如何使用:httpc:
headers =
if apikey,
do: [{'Content-Type', 'application/json'}, {'apikey', to_charlist(apikey)}],
else: [{'Content-Type', 'application/json'}]
http_options = [timeout: @timeout, connect_timeout: @timeout]
options = []
request = {
to_charlist(url),
headers,
'application/json',
to_charlist(encoded_obj)
}
:post
|> :httpc.request(request, http_options, options)
|> handle_response()
我收到很多错误,例如:
=SUPERVISOR REPORT==== 6-Mar-2018::15:44:11 ===
Supervisor: {local,httpc_handler_sup}
Context: child_terminated
Reason: {function_clause,
[{http_transport,close,
[undefined,#Port<0.21557>],
[{file,"http_transport.erl"},{line,346}]},
{gen_server,try_terminate,3,
[{file,"gen_server.erl"},{line,648}]},
{gen_server,terminate,10,
[{file,"gen_server.erl"},{line,833}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,247}]}]}
Offender: [{pid,<0.2596.1>},
{id,undefined},
{mfargs,{httpc_handler,start_link,undefined}},
{restart_type,temporary},
{shutdown,4000},
{child_type,worker}]
还有
15:44:11.743 [error] GenServer #PID<0.2595.1> terminating
** (FunctionClauseError) no function clause matching in :http_transport.close/2
(inets) http_transport.erl:346: :http_transport.close(:undefined, #Port<0.21559>)
(stdlib) gen_server.erl:648: :gen_server.try_terminate/3
(stdlib) gen_server.erl:833: :gen_server.terminate/10
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:init_error, :error_sending, {#Reference<0.18155839.2531262466.203553>, {:error, :einval}}}
相同的错误报告不同。
这句话说了一些我也不太明白的东西:
15:44:11.741 [error] Bad value on output port 'tcp_inet'
我真的不明白为什么会这样。
我使用的是HTTPotion,但没有这个问题(虽然有其他问题)。
问题是这适用于我的开发机器。它也适用于我机器上的类似生产的虚拟机。但是当它在真正的生产服务器上运行时会抛出这个错误。
我很困惑!
【问题讨论】:
-
错误是什么?
-
对不起@Dogbert,我不小心点击了帖子:)
-
在 erlang 中,当正文格式化为 json 时,我可以成功向 apache 服务器发出 httpc cgi post 请求,但是当正文被格式化时,我无法成功向 inets httpd 服务器发出 httpc cgi post 请求被格式化为 json。当正文格式化为
x-www-form-urlencoded时,我能够成功地向 inets httpd 服务器发出 httpc cgi post 请求。你会遇到类似的事情吗? -
@7stud 这很奇怪。但是端点没有使用httpd,它是红隼。我想我需要比较 dev 和 prod erlang 版本。如果可以的话,我会用更多信息更新问题。