HTTP POST and GET using cURL in Linux [dupliquer]

cette question a déjà une réponse ici:

  • Comment envoyer un en-tête en utilisant une requête HTTP via un appel curl? 7 réponses

j'ai eu une application serveur dans asp.net dans windows, j'avais un service web pour ça.

Comment puis-je appeler le service web sous Linux utiliser le script shell en utilisant la commande cURL?

294
demandé sur ROMANIA_engineer 2013-02-20 15:11:26

2 réponses

*nix offre une belle petite commande qui rendent notre vie beaucoup plus facile.

GET:

avec JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

avec XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

Pour l'affichage des données:

curl --data "param1=value1&param2=value2" http://hostname/resource

Pour le fichier de téléchargement:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

Pour la connexion à un site (auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

impression le curl résultats:

pour JSON:

Si vous utilisez npm et nodejs , vous pouvez installer le paquet json en exécutant cette commande:

npm install -g json

Utilisation:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json

si vous utilisez pip et python , vous pouvez installer pjson paquet en exécutant cette commande:

pip install pjson

Utilisation:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson

si vous utilisez Python 2.6+, l'outil json est inclus.

Utilisation:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool

si vous utilisez gem et ruby , vous pouvez installer colorful_json paquet en exécutant cette commande:

gem install colorful_json

Utilisation:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson

si vous utilisez apt-get (aptitude package manager de votre distribution Linux), vous pouvez installer yajl-tools package en exécutant cette commande:

sudo apt-get install yajl-tools

Utilisation:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource |  json_reformat

XML:

si vous utilisez * nix avec Debian/Gnome environnement, installez libxml2-utils :

sudo apt-get install libxml2-utils

Utilisation:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -

ou installer tidy :

sudo apt-get install tidy

Utilisation:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | tidy -xml -i -

Enregistrer la réponse en boucle à un fichier

curl http://hostname/resource >> /path/to/your/file

ou

curl http://hostname/resource -o /path/to/your/file

pour une description détaillée de la boucle commande, appuie:

man curl

pour plus de détails sur les options / commutateurs de la commande curl, cliquez sur:

curl -h
600
répondu Amith Koujalgi 2017-06-27 09:06:26

je pense Qu'Amith Koujalgi a raison, mais aussi, dans les cas où les réponses de webservice sont dans JSON, il pourrait être plus utile de voir les résultats dans un format propre JSON au lieu d'une chaîne très longue. Il suffit d'ajouter / grep } / python-mjson.voici deux exemples:

OBTENIR de l'approche avec le résultat JSON

curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool 

POST approche avec le résultat 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

enter image description here

47
répondu CPU 100 2014-01-21 21:00:01