例如,在运行我的简单代码时,我已经安装了Cro模块:
my %headers = {Authorization => OAuth realm="", oauth_consumer_key="xxxxxxxxxxxxxxxx", oauth_nonce="29515362", oauth_signature="KojMlteEAHlYjMcLc6LFiOwRnJ8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1525913154", oauth_token="xxxx-xxxxxxxxxxxxxxxxxx", oauth_version="1.0", User-Agent => Cro};
my $resp = await Cro::HTTP::Client.get: 'http://api.fanfou.com/statuses/home_timeline.json',
headers => [
user-agent => 'Cro',
content-type => 'application/json;charset=UTF-8',
|%headers
];
say $resp.header('content-type'); # Output: application/json; charset=utf-8;
my Str $text = await $resp.body-text(); 它说‘无法解析媒体类型的application/json; charset=utf-8;’
Died with the exception:
Could not parse media type 'application/json; charset=utf-8;'
in method parse at /Users/ohmycloud/.perl6/sources/5B710DB8DF7799BC8B40647E4F9945BCB8745B69 (Cro::MediaType) line 74
in method content-type at /Users/ohmycloud/.perl6/sources/427E29691A1F7367C23E3F4FE63E7BDB1C5D7F63 (Cro::HTTP::Message) line 74
in method body-text-encoding at /Users/ohmycloud/.perl6/sources/427E29691A1F7367C23E3F4FE63E7BDB1C5D7F63 (Cro::HTTP::Message) line 83
in block at /Users/ohmycloud/.perl6/sources/F870148C579AB45DEB39F02722B617776C3D6D5F (Cro::MessageWithBody) line 49application/json; charset=utf8;似乎不是一个有效的content-type,因此我添加了一个测试:
use Cro::MediaType;
use Test;
sub parses($media-type, $desc, &checks) {
my $parsed;
lives-ok { $parsed = Cro::MediaType.parse($media-type) }, $desc;
checks($parsed) if $parsed;
}
parses 'application/json; charset=utf-8;', 'application/json media type with charset', {
is .type, 'application', 'Correct type';
is .subtype, 'json', 'Correct subtype';
is .subtype-name, 'json', 'Correct subtype name';
is .tree, '', 'No tree';
is .suffix, '', 'No suffix';
is .Str, 'application/json; charset=utf-8;', 'Stringifies correctly';
};
done-testing;产出如下:
not ok 1 - application/json media type with charset
# Failed test 'application/json media type with charset'
# at cro_media.pl6 line 6
# Could not parse media type 'application/json; charset=utf-8;'
1..1
# Looks like you failed 1 test of 1源代码似乎位于/Users/ohmycloud/.perl6/sources/5B710DB8DF7799BC8B40647E4F9945BCB8745B69文件中,我在TOP令牌之后添加了';'?:
token TOP { <media-type> ';'? }保存,然后再次运行我的代码,但是错误是相同的。那么,如何使改变有效呢?在Perl 5中,我只需编辑我的.pm模块,但在Perl 6中,我不知道该做什么。
发布于 2018-05-10 05:27:01
在zef问题中的这个答案中,它们指出“安装是不可变的”。如果您从它的源代码下载Cro,修补它并重新安装,以便您的应用程序获得新版本,这可能是一个更好的选择。
也可能发生'application/json‘不承认字符集声明,或者不应该在;后面有空格。但这里的主要问题是,一旦安装了模块,就不应该编辑模块。
发布于 2018-05-10 10:28:18
正如jjmerelo所提到的,安装是不可变的,一个解决方案是下载源代码(包括META6.json文件),编辑所需的代码,然后:
zef install . --/test为了简单的测试,我没问题。
至于application/json; chartset=utf-8;无法解析,我在MediaType.pm6的;令牌中添加了一个;,使得包含;成为可能(可能是一个bug,我不知道):
token token { <[A..Za..z0..9;!#$%&'*+^_`{|}~-]>+ }在本地安装,现在解析ok。
https://stackoverflow.com/questions/50264214
复制相似问题