Pregunta

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;
}
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top