How to pass payload via JSON file for curl? -
i can create place via curl
executing following command:
$ curl -vx post https://server/api/v1/places.json -d " auth_token=b8dsbz4hexmskqua6qhn& \ place[name]=fuelstation central& \ place[city]=grossbeeren& \ place[address]=buschweg 1& \ place[latitude]=52.3601& \ place[longitude]=13.3332& \ place[washing]=true& \ place[founded_at_year]=2000& \ place[products][]=diesel& \ place[products][]=benzin \ "
the server returns http/1.1 201 created
.
want store payload in json file looks this:
// testplace.json { "auth_token" : "b8dsbz4hexmskqua6qhn", "name" : "fuelstation central", "city" : "grossbeeren", "address" : "buschweg 1", "latitude" : 52.3601, "longitude" : 13.3332, "washing" : true, "founded_at_year" : 2000, "products" : ["diesel","benzin"] }
so modify command executed this:
$ curl -vx post http://server/api/v1/places.json -d @testplace.json
this fails returning http/1.1 401 unauthorized
. why?
curl
sends post requests default content type of application/x-www-form-urlencoded
. if want send json request, have specify correct content type header:
$ curl -vx post http://server/api/v1/places.json -d @testplace.json \ --header "content-type: application/json"
but work if server accepts json input. .json
@ end of url may indicate output json, doesn't mean handle json input. api documentation should give hint wheather or not.
the reason why 401
, not other error because server can't extract auth_token
request.
Comments
Post a Comment