Search code examples
linuxcurlasteriskasteriskami

How can I work with AMI via cURL?


I have an asterisk server with AMI web enabled on port 8088, prefix asterisk. So my rawman url would be http://myserver:8088/asterisk/rawman

I just tried to login and ping with 2 urls
http://myserver:8088/asterisk/rawman?action=login&username=admin&secret=admin
http://myserver:8088/asterisk/rawman?action=ping

It works when I enter 2 above urls into the web browsers, but it does not when I use cURL from terminal.

Result from Terminal:
curl 'http://myserver:8088/asterisk/rawman?action=login&username=admin&secret=admin'
Response: Success
Message: Authentication accepted

curl 'http://myserver:8088/asterisk/rawman?action=ping'
Response: Error
Message: Permission denied

Error from Asterisk CLI: (X.X.X.X is my LAN ip address)

HTTP Connect attempt from 'X.X.X.X' unable to authenticate

So I can not execute any further command like the way I did when dealing with AJAM.
If anybody have an idea, please help me out, thank you.
Best Regards
Loi Dang


Solution

  • With your first request in browser you pass through authentication process and Asterisk respond with a cookie that your browser keeps and uses in further requests. You should see it in AMI response Set-Cookie header.

    However that doesn't happen when you use cURL, it doesn't save the cookie for you. You should explicitly tell cURL to use the cookie in the request. Refer to cURL manual to find details http://curl.haxx.se/docs/http-cookies.html

    OR

    You can provide digest authentication for every single request with the alternate a* commands. For instance your ping action will look like this:

    curl -v --digest -u admin:admin http://myserver:8088/asterisk/arawman?action=ping