This simple example shows us how to consume Basic Auth API from command line.


Information


Assuming that you use headers below to consume API in Chrome Postman extension.


Authorization: Basic c3R1ZGVudDpzdHVkZW50
Content-Type: application/json

Command


As you can see below, we actually use plain username and password instead of Authorization header. Curl then creates Authorization: Basic c3R1ZGVudDpzdHVkZW50 itself.


curl -v -X GET -u student:student -H "Content-Type: application/json" "http://webservice.local/app_test.php/api/student"

* Hostname was NOT found in DNS cachehp/api/student"
* Trying 127.0.0.1...
* Connected to webservice.local (127.0.0.1) port 80 (#0)
* Server auth using Basic with user 'student'
> GET /app_test.php/api/student HTTP/1.1
> Authorization: Basic c3R1ZGVudDpzdHVkZW50
> User-Agent: curl/7.37.1
> Host: webservice.local
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 200 OK
< Date: Wed, 30 Dec 2015 10:08:56 GMT
* Server Apache/2.4.10 (Unix) PHP/5.6.9 is not blacklisted
< Server: Apache/2.4.10 (Unix) PHP/5.6.9
< Vary: Authorization
< X-Powered-By: PHP/5.6.9
< Cache-Control: no-cache
< Content-Length: 25
< Content-Type: application/json
<
* Connection #0 to host webservice.local left intact

"Hello ROLE_STUDENT user"

Alternatively, you can do with the one below directly as well.


curl -v -X GET -H "Authorization: Basic c3R1ZGVudDpzdHVkZW50" "Content-Type: application/json" "http://webservice.local/app_test.php/api/student"