[英]Elixir httpc error on production
我想使用:httpc
作为我的HTTP客户端。
我将edeliver用于发布管理等。 我的.deliver/config.exs
有:inets
和:httpc
:
set applications: [
:runtime_tools,
:inets,
:httpc
]
我还补充:inets
到:extra_applications
在mix.exs
。
这是我的使用方式: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
,但是没有这个问题(尽管有其他问题)。
问题是这可以在我的开发机器上工作。 它也可以在我的机器上的类似生产的VM上运行。 但是当它在实际生产服务器上运行时会引发此错误。
我很混乱!
在erlang中, httpc:request()
函数有一个Request参数变量:
request(Method, Request, HTTPOptions, Options) ->
Request参数变量的类型指定为:
Request = request()
request() = {url(), headers()}
其中url() = string()
和headers() = [header()]
,即一个列表,因此Request参数的形状需要为:
{string, []}
但是,您正在这样做:
request = {
to_charlist(url),
headers,
'application/json',
to_charlist(encoded_obj)
}
看起来像是:
{string, [], string, string}
我不知道为什么要在标头列表之外复制“ application / json”的Content-Type,而且我不清楚to_charlist(encoded_obj)
应该是什么。 我会尝试:
request = {
to_charlist(url),
headers
}
如果to_charlist(encoded_obj)
必须位于http标头中,则将适当的键/值对添加到标头列表中。 可能的http标头在这里 。 也许您需要:
headers =
if api,
do: [{'Content-Type', 'application/json'},
{'apikey', to_charlist(apikey)},
{'Authorization, to_charlist(encoded_obj)} ],
else: [{'Content-Type', 'application/json'},
{'Authorization, to_charlist(encoded_obj)} ]
最好写成:
base_headers = [
{'Content-Type', 'application/json'},
{'Authorization', to_charlist(encoded_obj)}
]
request_headers =
if apikey,
do: [ {'apikey', to_charlist(apikey)} | base_headers ],
else: base_headers
问题是这可以在我的开发机器上工作。 它也可以在我的机器上的类似生产的VM上运行。 但是当它在实际生产服务器上运行时会引发此错误。
好吧,然后看这里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.