I'm learning Apigility (Apigility docu -> REST Service Tutorial) and trying to send a POST request with basic authentication via cURL:
$ curl -X POST -i -H "Content-Type: application/hal+json" -H "Authorization: Basic YXBpdXNlcjphcGlwd2Q=" http://apigilityhw.sandbox.loc/status
YXBpdXNlcjphcGlwd2Q=
is the base 64 encoded string with my credentials apiuser:apipwd
. The credentials are saved in the /data/htpasswd
(apiuser:$apr1$3J4cyqEw$WKga3rQMkxvnevMuBaekg/
).
The looks like this:
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.7
Date: Mon, 22 Sep 2014 07:48:47 GMT
Content-Type: application/problem+json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.12-1~dotdeb.1
WWW-Authenticate: Basic realm="api"
Where is the mistake here? How to get it work?
curl -u username:password http://
curl -u username http://
From the documentation page:
-u, --user
http://curl.haxx.se/docs/manpage.html#-u
Note that you do not need --basic
flag as it is the default.
as header
AUTH=$(echo -ne "$BASIC_AUTH_USER:$BASIC_AUTH_PASSWORD" | base64 --wrap 0)
curl \
--header "Content-Type: application/json" \
--header "Authorization: Basic $AUTH" \
--request POST \
--data '{"key1":"value1", "key2":"value2"}' \
https://example.com/
The easiest way to figure out what authorization header should look like might be first to run curl with -u (or putting the credentials within the URL) and -v and the output will show the request header:
$ curl -v -u 'apiuser:apipwd' ... http://apigilityhw.sandbox.loc/status
# OR putting the credentials in the URL:
$ curl -v ... http://apiuser:apipwd@apigilityhw.sandbox.loc/status
# copy and paste the "Authorization" header from the output:
$ curl -H 'Authorization: Basic YWRtaW46YXBpcHdk' ... http://apigilityhw.sandbox.loc/status
Success story sharing