421 votes

HTTP POST et GET avec cURL sous Linux

J'ai une application serveur écrite en ASP.NET sur Windows qui fournit un service web.

Comment puis-je appeler le service web sous Linux avec cURL ?

3 votes

Pour construire rapidement une commande curl, j'utilise cet outil en ligne : curlbuilder.com

810voto

Amith Koujalgi Points 2005

*nix fournit une jolie petite commande qui rend nos vies beaucoup plus faciles.

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'enregistrement des données :

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

Pour le téléchargement de fichiers :

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

Poste HTTP RESTful :

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

Pour se connecter à un site (auth) :

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

Jolie impression des résultats de bouclage :

Pour JSON :

Si vous utilisez npm y nodejs vous pouvez installer 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 y python vous pouvez installer pjson 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 fourni avec.

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 y ruby vous pouvez installer colorful_json 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 (gestionnaire de paquets aptitude de votre distro Linux), vous pouvez installer yajl-tools 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

Pour XML :

Si vous utilisez *nix avec l'environnement Debian/Gnome, 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 -

Sauvegarde de la réponse curl dans un fichier

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

o

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

Pour une description détaillée de la commande curl, cliquez sur :

man curl

Pour plus de détails sur les options/interrupteurs de la commande curl, cliquez sur :

curl -h

1 votes

Si vous redirigez vers less vous devrez peut-être ajouter -sS .

0 votes

Comment puis-je obtenir la réponse à mettre dans une autre ligne de commande ?

0 votes

@emoleumassi voir le commentaire de x-yuri avant le vôtre ; vous devriez être en mesure de canaliser le retour dans une autre commande, telle que less .

59voto

CPU 100 Points 2269

Je pense qu'Amith Koujalgi a raison mais aussi, dans les cas où les réponses du webservice sont en JSON, il pourrait être plus utile de voir les résultats dans un format JSON propre plutôt que dans une très longue chaîne. Il suffit d'ajouter | grep }| python -mjson.tool à la fin des commandes curl : voici deux exemples :

Approche GET avec résultat JSON

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

Approche POST avec 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

3 votes

Qu'est-ce que grep est censé faire ici ? J'utilise ce qui suit avec le même résultat : curl -s -H "Accept: application/json" -H "Content-Type: application/json" -H "http://${API_BASE_URL}${1}" | python -mjson.tool

6voto

R Square Points 1155

boucle d'homme :

   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H "Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they will only mess things up for you.

          See also the -A/--user-agent and -e/--referer options.

          This option can be used multiple times to add/replace/remove  multi-
          ple headers.

Exemple :

**curl --header "X-MyHeader: 123" www.google.com**

Vous pouvez voir la requête que curl a envoyée en ajoutant l'attribut -v option.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X