Я создал функцию Perl для отправки данных
Код: Выделить всё
#!/usr/bin/perl
use strict;
use warnings;
use JSON qw(encode_json);
use AnyEvent;
use AnyEvent::HTTP;
=pod
Function send asynchronus data , get response code
=cut
sub sendOutboundPostRequestAsync {
my ($url, $data, $token) = @_;
# Create a condition variable to wait for the asynchronous request
my $request_done = AnyEvent->condvar;
# Convert the data hash to JSON
my $json_data = encode_json($data);
print "JSON data being sent: $json_data\n"; # For debugging
# Make the HTTP POST request asynchronously
http_post $url,
headers => {
'Authorization' => $token,
'Content-Type' => 'application/json',
},
body => $json_data,
sub {
my ($body, $headers) = @_;
my $status_code = $headers->{Status} // 500; # Default to 500 if no status
# Print the response for debugging
print "Response Body: $body\n";
print "Request completed with status code: $status_code\n";
# Send the status code back through the condition variable
$request_done->send($status_code);
};
return $request_done; # Return the condition variable for further processing
}
#####
my $url = 'http://xxx.xxx.xxx/test.php';
my $token = 'eyJ0eXAiOiJKV1QiLCJhbGci';
# Example data to send
my %data = (
uniqueid => '1727331196.gesti',
start_time => '2024-09-26 08:13:17',
);
# Call the function
my $response_code_condvar = sendOutboundPostRequestAsync($url, \%data, $token);
my $response_code = $response_code_condvar->recv; # Wait for the request to complete
print "Final Response Code: $response_code\n";
Код: Выделить всё
После выполнения сценария Perl я получаю
Код: Выделить всё
ast1:~ # perl test.agi
JSON data being sent: {"start_time":"2024-09-26 08:13:17","uniqueid":"1727331196.gesti"}
Response Body: Invalid JSON
Request completed with status code: 400
Final Response Code: 400
Код: Выделить всё
Received POST data:
headers
- Это лучший способ отправлять асинхронные данные с помощью Perl?
- В чем может быть проблема с пустым результатом данных?
Подробнее здесь: https://stackoverflow.com/questions/790 ... an-php-url
Мобильная версия