1
votes

SendHub's documentation state to send a post with data and json data. This is the curl example they gave Trying to do this with perl and LWP::User Agent but getting either bad request or unauthorized

Do I have the request coded properly?

curl -H "Content-Type: application/json" -X POST --data '{"contacts" : [1111],"text" : "Testing"}' https://api.sendhub.com/v1/messages/?username

require LWP::UserAgent;  

my $uri = 'https://api.sendhub.com/v1/messages/?username=MY_USERNAME\&api_key=MY_KEY_NUMBER';
my $json = '{"contacts":[18005551212],"text":"testing"}';
my $req = HTTP::Request->new('POST',$uri);
$req->header('Content-Type' => 'application/json');
$req->content($json);

my $lwp = LWP::UserAgent->new;
my $response=$lwp->request($req);

if ($response->is_success) {
  print $response->decoded_content;  
}
else {
  die $response->status_line;
}
1
You should ordinarily use LWP::UserAgent rather than using require, unless you have a good reason to do otherwise. That applies to all modules.Borodin

1 Answers

1
votes

It looks basically OK.

What is the backslash before the ampersand in the URL?

'https://api.sendhub.com/v1/messages/?username=MY_USERNAME\&api_key=MY_KEY_NUMBER'

I think it should be

'https://api.sendhub.com/v1/messages/?username=MY_USERNAME&api_key=MY_KEY_NUMBER'

but if you're getting 401 Unauthorized then it's most likely the request is correct but the user name and key are wrong.