英文:
bad argument in gen_tcp recv when split
问题
Hi,我正在阅读一个请求,我想要获取路径,但是当我运行这段代码
{:ok, line} = :gen_tcp.recv(socket, 0)
IO.inspect "************************* #{inspect line}"
[_server_name, verb, info_path, _version] = String.split(line)
其中 inspect 看起来正常,有 4 个参数
{:http_request, :GET, {:abs_path, "/gg"}, {1, 1}}
错误提示说:
{badarg,[{binary,split,
[{http_request,'GET',{abs_path,<<"gg">>},{1,1}},
[<<227,128,128>>,
<<226,129,159>>,
<<226,128,169>>,
<<226,128,168>>,
<<226,128,128>>,
<<226,128,129>>,
<<226,128,130>>,
<<226,128,131>>,
<<226,128,132>>,
<<226,128,133>>,
<<226,128,134>>,
<<226,128,136>>,
<<226,128,137>>,
<<226,128,138>>,
<<225,154,128>>,
<<194,133>>,
<<"\t">>,
<<"\n">>,
<<"\v">>,
<<"\f">>,
<<"\r">>],
[global,trim_all]],
[]},
我不知道那些额外的参数是什么。
如果我尝试绑定整个行而不使用分割,我会收到
[_server_name, verb, info_path, _version] = line
{badmatch,{http_request,'GET',{abs_path,<<"gg">>},{1,1}}}
英文:
Hi I´m reading a request where I want to get the path, but when I run this code
{:ok, line} = :gen_tcp.recv(socket, 0)
IO.inspect "************************* #{ inspect line} "
[_server_name, verb, info_path, _version] = String.split(line)
Where the inspect looks ok with 4 arguments
{:http_request, :GET, {:abs_path, \"/gg\"}, {1, 1}}
The error said:
{badarg,[{binary,split,
[{http_request,'GET',{abs_path,<<"/gg">>},{1,1}},
[<<227,128,128>>,
<<226,129,159>>,
<<226,128,169>>,
<<226,128,168>>,
<<226,128,128>>,
<<226,128,129>>,
<<226,128,130>>,
<<226,128,131>>,
<<226,128,132>>,
<<226,128,133>>,
<<226,128,134>>,
<<226,128,136>>,
<<226,128,137>>,
<<226,128,138>>,
<<225,154,128>>,
<<194,133>>,
<<" ">>,<<"\t">>,<<"\n">>,<<"\v">>,<<"\f">>,<<"\r">>],
[global,trim_all]],
[]},
I don't know what are all those extra arguments of the line.
If I try just bind the line with every type without the split I receive
[_server_name, verb, info_path, _version] = line
{{badmatch,{http_request,'GET',{abs_path,<<"/gg">>},{1,1}}},
答案1
得分: 2
如果我正确阅读:gen_tcp.recv
的文档,您“应该”会收到一个HttpPacket
,在您的情况下似乎是一个HttpRequest
。由于请求的解析已为您处理,您“应该”能够简单地执行以下操作:
{:ok, line} = :gen_tcp.recv(socket, 0)
{_server_name, verb, info_path, _version} = line
注意: HttpRequest
使用{...}
大括号而不是[...]
方括号。
英文:
If I read the docs of :gen_tcp.recv
correctly, you "should" receive a HttpPacket
back, which in your case seems to be a HttpRequest
. Since the parsing of the request is handled for you, you "should" be able to simply do:
{:ok, line} = :gen_tcp.recv(socket, 0)
{_server_name, verb, info_path, _version} = line
Note: The HttpRepuest
uses {...}
curly brackets and not [...]
brackets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论