Bu basit örnek ile bir Basic Auth API'yi curl ile nasıl kullanabiliriz, onu göreceğiz.


Bilgi


Varsayalım ki aşağıdaki hederler ile API'yi Chrome Postman kullanarak tüketiyoruz.


Authorization: Basic c3R1ZGVudDpzdHVkZW50
Content-Type: application/json

Komut


Aşağıda gördüğümüz gibi aslında kullanıcı adı ve şifre kullanıyoruz. Curl daha sonra bu bilgileri kullanarak, kendiliğinden Authorization: Basic c3R1ZGVudDpzdHVkZW50 header yaratıyor.


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"

Alternatif olarak aşağıdaki örneğide kullanabilirsiniz.


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