我阅读了LWP文档、API文档中的原语,以及其他语言的大量示例。我不能让POST在Perl中工作。一切正常。POST在curl或Python中工作正常,但我找不到Perl的正确语法。
我总是得到{“code”:40010000,“message”:“request body format is invalid”}总是。
当然是在沙盒里工作。下面是我尝试过的一些子程序。
use LWP::UserAgent;
my vars...
while ($line = <POSITIONS>) {
($ticker, $date8601, $epoch, $side, $qty0, $entryprice, $unrealized) = split(",", $line);
sellorder5($url, $API, $secret, $ticker, $qty0);
}
sub sellorder5 {
$req=HTTP::Request->new(POST=>$url);
$req->header( 'APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
$req->content('{"symbol":"$ticker","qty":"$qty0","side":"sell","type":"market","time_in_force":"gtc"}');
$resp = $browser->request($req);
$raw_text = $resp->decoded_content;
}
sub sellorder4 {
$req = HTTP::Request->new(POST => $url); # Create a request
$req->header( 'APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
$req->content('{"symbol":"$ticker", "qty":"$qty0", "side":"sell", "type":"market", "time_in_force":"gtc"}');
$resp = $browser->request($req);
$raw_text = $resp->content;
}
sub sellorder3 {
$req = HTTP::Request->new(POST => $url);
$req->header( 'APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
$post_data = '{ "symbol":"$ticker", "qty":"$qty0", "side":"sell", "type":"market", "time_in_force":"gtc" }';
$req->content($post_data);
$resp = $browser->request($req);
$raw_text = $resp->decoded_content;
}
sub sellorder2 {
my $req = HTTP::Request->new(POST => $url);
$req->header( 'APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret);
$post_data = '{ "symbol":"$ticker", "qty" : $qty0, "side" : "sell", "type" : "market", "time_in_force" : "gtc" }';
$req->content($post_data);
$response = $browser->request($req);
$raw_text = $response->decoded_content;
}
sub sellorder {
$response = $browser->post($url, 'APCA-API-KEY-ID' , $API, 'APCA-API-SECRET-KEY' , $secret, [ 'symbol' => '$ticker', 'qty' => '$qty0', 'side' => 'sell', 'type' => 'market', 'time_in_force' => 'gtc' ] );
$raw_text = $response->content;
$raw_text =~ s/[\x00-\x1f]//g; #remove stray characters, and make into one line
}
字符串
如果有人有一个工作的例子,我会很感激。- 谢谢-谢谢
1条答案
按热度按时间c3frrgcw1#
你的问题是这一行:
字符串
单引号用于字符串 *,不带插值 *。所以
$ticker
将不是$ticker
变量的内容,而是一个由美元符号和单词“ticker”组成的字符串。双引号将插入变量,所以:
型
当然,那很难看。因此,您可以使用
qq//
,类似引号的插值运算符。型
但更好的是,不要手写JSON。使用JSON::PP或类似的格式。
型