zoukankan      html  css  js  c++  java
  • linux基本操作(七)

    CURL使用

    一、简介

      curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。

      语法:

    curl [options] [URL...]
    

      不带有任何参数时,curl 就是发出 GET 请求。

    $ curl https://www.example.com
    

      上面命令向www.example.com发出 GET 请求,服务器返回的内容会在命令行输出。

    -A

      -A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]

    $ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
    

      上面命令将User-Agent改成 Chrome 浏览器。

    $ curl -A '' https://google.com
    

      上面命令会移除User-Agent标头。也可以通过-H参数直接指定标头,更改User-Agent

    $ curl -H 'User-Agent: php/1.0' https://google.com
    

    -b

      -b参数用来向服务器发送 Cookie。

    $ curl -b 'foo=bar' https://google.com
    

      上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。

    $ curl -b 'foo1=bar;foo2=bar2' https://google.com
    

      上面命令发送两个 Cookie。

    $ curl -b cookies.txt https://www.google.com
    

      上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

    -c

      -c参数将服务器设置的 Cookie 写入一个文件。

    $ curl -c cookies.txt https://www.google.com
    

      上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt

    -d

      -d参数用于发送 POST 请求的数据体。

    $ curl -d'login=emma&password=123'-X POST https://google.com/login
    # 或者
    $ curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login
    

      使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST-d参数可以读取本地文本文件的数据,向服务器发送。 

    $ curl -d '@data.txt' https://google.com/login
    

      上面命令读取data.txt文件的内容,作为数据体向服务器发送

    --data-urlencode

      --data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

    $ curl --data-urlencode 'comment=hello world' https://google.com/login
    

      上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

    -e

      -e参数用来设置 HTTP 的标头Referer,表示请求的来源。

    curl -e 'https://google.com?q=example' https://www.example.com
    

      上面命令将Referer标头设为https://google.com?q=example-H参数可以通过直接添加标头Referer,达到同样效果

    curl -H 'Referer: https://google.com?q=example' https://www.example.com
    

    -F

      -F参数用来向服务器上传二进制文件。

    $ curl -F 'file=@photo.png' https://google.com/profile
    

      上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。-F参数可以指定 MIME 类型。

    $ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
    

      上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream-F参数也可以指定文件名。

    $ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
    

      上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png

    还有一些其他的在这不列举参考下面文档:

      https://baike.baidu.com/item/curl/10098606?fr=aladdin

      http://www.ruanyifeng.com/blog/2019/09/curl-reference.html

      http://aiezu.com/article/linux_curl_command.html

      https://www.jianshu.com/p/fc0eb6c60816



      

      

      

    kafka rabbitMq
  • 相关阅读:
    mysql 8.x 忘记密码
    sqlite
    公租房查询记录
    idea 常用快捷键总结
    vue h5项目中上传图片时保存之后显示不全
    微信小程序开发中使用npm命令快速添加页面
    【Luogu7816】「Stoi2032」以父之名
    【XSY3657】 因数分解
    Min_25筛学习笔记
    拉格朗日反演学习笔记
  • 原文地址:https://www.cnblogs.com/stt101517/p/15348393.html
Copyright © 2011-2022 走看看