zoukankan      html  css  js  c++  java
  • curl tutorial with examples of usage

    原文:http://www.yilmazhuseyin.com/blog/dev/curl-tutorial-examples-usage/

    阮一峰的这个教程也不错:http://www.ruanyifeng.com/blog/2011/09/curl.html

    Curl is a linux utility that is used to make HTTP requests to a given url. It outputs HTTP response to standard output and is actually very easy to use. Here are some examples to show its usage:

    Make a GET request without any data:
    curl http://yilmazhuseyin.com/blog/dev/
    curl --request GET 'http://yilmazhuseyin.com/blog/dev/'

    Both usages are actually the same. However, I try to use second one all the time. With --request (or -X) parameter, we choose our http method to use for our requests. Its values can be GET, POST, DELETE, PUT etc.. If we don't specify this parameter, GET method will be used by default. That is why first version works same as the second one.

    Make requests with different HTTP method type without data:
     curl --request POST 'http://www.somedomain.com/'
     curl --request DELETE 'http://www.somedomain.com/'
     curl --request PUT 'http://www.somedomain.com/'
    Make requests with data:

    Since we learn how to make POST, GET, PUT, DELETE requests, we can now make same requests with data. In order to send data with those requests, we should use --data parameter. Here are some examples:

    ::::/bin/bash
    # send login data with POST request
    curl --request POST 'http://www.somedomain.com/login/' 
    --data 'username=myusername&password=mypassword'# send search data to with get request
    curl --request GET 'http://www.youtube.com/results?search_query=my_keyword'# send PUT request with data
    curl --request PUT 'http://www.somedomain.com/rest-api/user/12345/'
    --data 'email=myemail@gmail.com'# same thing but this one get data from a file named data.txt
    curl --request PUT 'http://www.somedomain.com/rest-api/user/12345/'
    --data @data.txt
    Make requests with extra headers

    Sometimes you need to add HTTP headers to your requests. This is done by --header parameter.

    curl --request GET 'http://www.somedomain.com/user/info/' 
    --header 'sessionid:1234567890987654321'

    Notice that we are using semicolon(":") to separate header name from its value.

    Get response with HTTP headers

    If you need to get HTTP headers with your response, --include parameter can be used

     curl --request GET 'http://somedomain.com/'--include

    Those examples cover most of the stuff curl is needed for. If you need more functionality, you can use following two commands as a reference

    # gives  brief description of parameters
    curl --help
    
    # curl manual page
    man curl

    TODO: add cookie parameters

  • 相关阅读:
    C#中Windows通用的回车转Tab方法
    对Form_Load事件的一点想法
    关于粉笔灰对教师影响的解决方案
    今天才发现MSSQLServer2000的排序功能原来这样
    C# 2.0与泛型
    (收藏)Anders Hejlsberg谈C#、Java和C++中的泛型
    对接口interface的一点想法
    马的遍历
    推荐软件:工作时间提醒器
    pgpoolII 介绍
  • 原文地址:https://www.cnblogs.com/walkerwang/p/3627365.html
Copyright © 2011-2022 走看看