使用以下命令的等效 POST 请求是什么:
对于这个curl shell命令:
curl --request POST \
--url https://api.someservice.com/v1/ \
--header 'Authorization: Bearer TOKEN' \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/audio.mp3 \
--form transformer=trans-2 \
--form format=text
Run Code Online (Sandbox Code Playgroud)
我尝试按照“Cro::HTTP::Client”中的示例进行操作, 但没有成功......
(参见以 开头的示例my $resp = await Cro::HTTP::Client.post: 'we.love.pand.as/pandas',)
这是一个使用@jja 答案的工作示例,需要:
拥有 OpenAI(帐户和)授权密钥
从此处下载 MP3 文件之一
use HTTP::Tiny;
my $fileName = $*HOME ~ '/Downloads/HelloRaccoonsEN.mp3';
say .<content>.decode given HTTP::Tiny.post: 'https://api.openai.com/v1/audio/transcriptions',
headers => { authorization => "Bearer {%*ENV<OPENAI_API_KEY>}" },
content => {
file => $fileName.IO,
model => 'whisper-1',
format => 'text'
};
Run Code Online (Sandbox Code Playgroud)
这是预期的结果:
# {"text":"Raku practitioners around the world, eat more onions!"}
Run Code Online (Sandbox Code Playgroud)
了解您尝试过什么,以便找出如何纠正它会很有帮助。无论如何,我希望示例请求在 Cro 中看起来像这样:
my $resp = await Cro::HTTP::Client.post: 'https://api.someservice.com/v1/',
headers => [ Authorization => 'Bearer TOKEN' ],
content-type => 'multipart/form-data',
body => [
transformer => 'trans-2',
format => 'text',
Cro::HTTP::Body::MultiPartFormData::Part.new(
headers => [Cro::HTTP::Header.new(
name => 'Content-type',
value => 'audio/mpeg'
)],
name => 'file',
filename => 'audio.mp3',
body-blob => slurp('/path/to/file/audio.mp3', :bin)
)
];
Run Code Online (Sandbox Code Playgroud)
我不确定到底什么哑剧类型curl适用,所以它可能与curl这方面的略有不同。
使用Raku的HTTP::Tinyish模块
我非常幸运地使用HTTP::Tinyish. 也许POST下面的代码适合您?
use HTTP::Tinyish;
my $http = HTTP::Tinyish.new(agent => "Mozilla/4.0");
my $server = 'https://rest.ensembl.org';
my $ext = '/archive/id';
my %response = $http.post: $server ~ $ext,
headers => {
'Content-type' => 'application/json',
'Accept' => 'application/json'
},
content => '{ "id" : ["ENSG00000157764", "ENSG00000248378"] }';
die "Failed: " ~ %response<status> ~ "\n" ~ %response<content> ~ "\n" unless %response<success>;
"\n1.________\n\n".put; #json output
use JSON::Fast <immutable>;
# .put for to-json(from-json(%response<content>));
"\n2.________\n\n".put; #below provides sorted output:
use PrettyDump;
for ( %response<content> ) {
my $pretty = PrettyDump.new;
put $pretty.dump: from-json(%response<content>);
};
"\n3.________\n\n".put; #below provides very compact `.raku` output:
# .say for from-json(%response<content>).raku;
Run Code Online (Sandbox Code Playgroud)
示例输出(PrettyDump仅限版本 2):
2.________
List=(
Map=(
:assembly("GRCh38"),
:id("ENSG00000157764"),
:is_current("1"),
:latest("ENSG00000157764.14"),
:peptide(Any),
:possible_replacement(List=()),
:release("109"),
:type("Gene"),
:version(14)
),
Map=(
:assembly("GRCh38"),
:id("ENSG00000248378"),
:is_current("1"),
:latest("ENSG00000248378.1"),
:peptide(Any),
:possible_replacement(List=()),
:release("109"),
:type("Gene"),
:version(1)
)
)
Run Code Online (Sandbox Code Playgroud)
注释掉/取消注释各种返回值(上面),直到找到适合您需要的返回值。