Ho un'applicazione server scritta in ASP.NET su Windows che fornisce un servizio web.
Come posso chiamare il servizio web in Linux con cURL?
* nix fornisce un piccolo comando piacevole che rende le nostre vite molto più semplici.
OTTENERE:
con JSON:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
con XML:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
POST:
Per la pubblicazione dei dati:
curl --data "param1=value1¶m2=value2" http://hostname/resource
Per il caricamento del file:
curl --form "[email protected]" http://hostname/resource
Post HTTP RESTful:
curl -X POST -d @filename http://hostname/resource
Per accedere a un sito (auth):
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
Abbastanza stampando i risultati del ricciolo:
Per JSON:
Se si utilizza npm
e nodejs
, è possibile installare il pacchetto json
eseguendo questo comando:
npm install -g json
Uso:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
Se si utilizza pip
e python
, è possibile installare il pacchetto pjson
eseguendo questo comando:
pip install pjson
Uso:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson
Se si utilizza Python 2.6+, lo strumento json è fornito in dotazione.
Uso:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool
Se usi gem
e Ruby
, puoi installare il pacchetto colorful_json
eseguendo questo comando:
gem install colorful_json
Uso:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson
Se si utilizza apt-get
(aptitude package manager della distro Linux), è possibile installare il pacchetto yajl-tools
eseguendo questo comando:
Sudo apt-get install yajl-tools
Uso:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json_reformat
Per XML:
Se usi * nix con Debian/Gnome envrionment, installa libxml2-utils
:
Sudo apt-get install libxml2-utils
Uso:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -
o installa tidy
:
Sudo apt-get install tidy
Uso:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | tidy -xml -i -
Salvare la risposta dell'arricciatura su un file
curl http://hostname/resource >> /path/to/your/file
o
curl http://hostname/resource -o /path/to/your/file
Per una descrizione dettagliata del comando di arricciatura, premi:
man curl
Per i dettagli sulle opzioni/opzioni del comando di arricciatura, premi:
curl -h
Penso che Amith Koujalgi sia corretto ma anche, nei casi in cui le risposte al webservice sono in JSON, potrebbe essere più utile vedere i risultati in un formato JSON pulito invece di una stringa molto lunga. Basta aggiungere | grep} | python -mjson.tool alla fine dei comandi di arricciatura qui sono due esempi:
Approccio GET con risultato JSON
curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool
POST approccio con risultato JSON
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool