Sunday, 18 August 2019

HTTPie: Basic Authentication


Syntax
http -a username:password {url}

For example the API '/api/v1/employees' is protected with basic authentication. If you try to access this secured API without providing proper credentials, you will get 'Unauthorized' error.

$http http://localhost:8080/api/v1/employees
HTTP/1.1 401 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 14:34:16 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=7787268B2C141F549F6587CEF3B7F6FA; Path=/; HttpOnly
Transfer-Encoding: chunked
WWW-Authenticate: Basic realm="Realm"
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/api/v1/employees",
    "status": 401,
    "timestamp": "2019-08-02T14:34:16.707+0000"
}


We can access the protected APIs by providing correct credentials.

$http -a krishna:password123 POST http://localhost:8080/api/v1/employees firstName=Krishna lastName=Gurram
HTTP/1.1 201 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 14:42:22 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=B77B7EC43A5C09E4E77A8F01B7D7C19B; Path=/; HttpOnly
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
    "firstName": "Krishna",
    "id": 5,
    "lastName": "Gurram"
}

$
$
$http -a krishna:password123 http://localhost:8080/api/v1/employees
HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Fri, 02 Aug 2019 14:42:32 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=4AE323F778D04EAEFC1269CFEF4380D8; Path=/; HttpOnly
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

[
    {
        "firstName": "Krishna",
        "id": 5,
        "lastName": "Gurram"
    }
]




Previous                                                    Next                                                    Home

No comments:

Post a Comment