Ik heb een servertoepassing geschreven in ASP.NET op Windows die een webservice biedt.
Hoe kan ik de webservice in Linux aanroepen met cURL?
Antwoord 1, autoriteit 100%
*nix biedt een leuke kleine opdracht die ons leven een stuk gemakkelijker maakt.
KRIJG:
met JSON:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
met XML:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
POST:
Voor het plaatsen van gegevens:
curl --data "param1=value1¶m2=value2" http://hostname/resource
Voor het uploaden van bestanden:
curl --form "[email protected]" http://hostname/resource
RESTful HTTP-bericht:
curl -X POST -d @filename http://hostname/resource
Om in te loggen op een site (auth):
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
De krulresultaten mooi afdrukken:
Voor JSON:
Als u npm
en nodejs
gebruikt, kunt u het json
-pakket installeren door deze opdracht uit te voeren:
npm install -g json
Gebruik:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
Als u pip
en python
gebruikt, kunt u het pjson
-pakket installeren door deze opdracht uit te voeren:
pip install pjson
Gebruik:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson
Als u Python 2.6+ gebruikt, wordt JON-tool gebundeld binnenin.
Gebruik:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool
Als u gem
en ruby
gebruikt, kunt u colorful_json
Package installeren door deze opdracht uit te voeren:
gem install colorful_json
Gebruik:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson
Als u apt-get
(Aptitude Package Manager van uw Linux Distro) gebruikt, kunt u yajl-tools
Package installeren door deze opdracht uit te voeren:
sudo apt-get install yajl-tools
Gebruik:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json_reformat
voor XML:
Installeer libxml2-utils
:
als u * NIX gebruikt met Debian / Gnome Encrionment
sudo apt-get install libxml2-utils
Gebruik:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -
of installeer tidy
:
sudo apt-get install tidy
Gebruik:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | tidy -xml -i -
De krulrespons opslaan op een bestand
curl http://hostname/resource >> /path/to/your/file
of
curl http://hostname/resource -o /path/to/your/file
Voor een gedetailleerde beschrijving van het curl-commando, druk op:
man curl
Voor details over opties/schakelaars van het curl-commando, druk op:
curl -h
Antwoord 2, autoriteit 7%
Ik denk dat Amith Koujalgi gelijk heeft, maar ook, in gevallen waarin de webservice-antwoorden in JSON zijn, kan het nuttiger zijn om de resultaten in een schoon JSON-formaat te zien in plaats van een zeer lange reeks. Voeg gewoon | . toe grep }| python -mjson.tool aan het einde van curl-commando’s zijn hier twee voorbeelden:
GET-aanpak met JSON-resultaat
curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool
POST-aanpak met JSON-resultaat
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool