码迷,mamicode.com
首页 > Web开发 > 详细

[译]httpie: CLI, 类似cURL的客户端

时间:2016-10-07 13:24:35      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

5 Request URL

HTTPie执行请求,唯一需要指定的信息是URL。默认的scheme,不出意外的,http://,是可以忽略的,http example.org 也是可以的。

此外,像curl一样的速记方式也是支持的。比如:3000也就是http://localhost:3000 如果忽略了端口,默认使用80

$ http :/foo
GET /foo HTTP/1.1
Host: localhost
$ http :3000/bar
GET /bar HTTP/1.1
Host: localhost:3000
$ http :
GET / HTTP/1.1
Host: localhost

如果需要在终端手动用查询字符串参数,可以用param=value的语法拼接URL参数,而不必担心会丢失&分隔符。而且,参数里的特殊字符也会自动去除。

可以用 --default-scheme <URL_SCHEME>选项来创建其他协议的快捷方式

$ alias https=‘http --default-scheme=https‘

  

6 请求项

有几种不同的request类型可以方便的指定HTTP头,JSON格式的数据,FORM数据,文件以及URL参数。在URL后面指定键值对。

这些请求项都有共同的特点,是实际请求的一部分,它们仅仅以分隔符区分,:=:===@=@, and :=@,有@符号的是指定一个文件的路径

Item TypeDescription

HTTP Headers

Name:Value

任意 HTTP header, e.g. X-API-Token:123.

URL parameters

name==value

添加键值对作为URL的查询字符串参数

The == separator is used.

Data Fields

field=value,field=@file.txt

请求数据字段默认作为JSON对象传递, 或者作为form-encoded (--form, -f).

Raw JSON fields

field:=json,field:=@file.json

有时传递JSON,有些字段类型是 Boolean,Number, 内嵌对象t, or an Array,

e.g., meals:=‘["ham","spam"]‘ or pies:=[1,2,3] (注意引号).

Form File Fields

field@/dir/file

仅仅刚使用 --form, -f的时候有效.

For example screenshot@~/Pictures/img.png. 这种形式会导致一个 a multipart/form-data request.

You can use \ to escape characters that shouldn‘t be used as separators (or parts thereof). For instance,foo\==bar will become a data key/value pair (foo= and bar) instead of a URL parameter.

有时需要用到引号把值引起来 e.g. foo=‘bar baz‘

如果字段名是以减号开始的‘-’(e.g., -fieldname), 需要把它们放在 -- 后面以避免冲突 --arguments:

$ http httpbin.org/post  --  -name-starting-with-dash=foo --Weird-Header:bar

  

POST /post HTTP/1.1
--Weird-Header: bar

{
    "-name-starting-with-dash": "value"
}

  注意数据字段并不是传递request data的唯一方式,Redirected input也可以传递唯一的数据 

7 JSON

JSON是现代web服务的通用语言,也是HTTPie默认使用的隐含类型:

如果命令行中包含数据项,它们默认序列化为JSON格式。HTTPie会自动设置下列头,它们也都是可以重写的:

Content-Type application/json
Accept application/json, */*

 

 

 也可以用--json, -j显式的把Accept设置成application/json,不管是否需要传递数据(有一种快捷方式设置头,通过常用的头标识符 http url Accept:‘application/json, */*‘)

此外,HTTPie会检测JSON响应,即使Content-Type并不正确(比如text/plain)或者没有设置。

例子:

$ http PUT example.org name=John email=john@example.org
PUT / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Content-Type: application/json
Host: example.org

{
    "name": "John",
    "email": "john@example.org"
}

非string的字段要用:=分隔符,可以把纯JSON插入对象里。Text和纯JSON文件可以分别用=@和:=@标识符插入:

$ http PUT api.example.com/person/1     name=John     age:=29 married:=false hobbies:=‘["http", "pies"]‘ \  # Raw JSON
    description=@about-john.txt \   # Embed text file
    bookmarks:=@bookmarks.json      # Embed JSON file
PUT /person/1 HTTP/1.1
Accept: application/json, */*
Content-Type: application/json
Host: api.example.com

{
    "age": 29,
    "hobbies": [
        "http",
        "pies"
    ],
    "description": "John is a nice guy who likes pies.",
    "married": false,
    "name": "John",
    "bookmarks": {
        "HTTPie": "http://httpie.org",
    }
}

用文件形式传递JSON数据用如下形式:

$ http POST api.example.com/person/1 < person.json

  

8 Form

8.1 规则form

$ http --form POST api.example.org/person/1 name=‘John Smith‘
POST /person/1 HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8

name=John+Smith

 

8.2 文件上传form

提供的一个以上的字段的话,序列化的content type就是multipart/form-data

$ http -f POST example.com/jobs name=‘John Smith‘ cv@~/Documents/cv.pdf

上述请求和如下HTML表单提交的情况一样:

<form enctype="multipart/form-data" method="post" action="http://example.com/jobs">
    <input type="text" name="name" />
    <input type="file" name="cv" />
</form>

注意@符号用于模拟文件上传的表单字段,而=@符号只是把文件内容作为text插入

 

9. HTTP 头

如果要设置自定义的头可以用Header:Value标志符

$ http example.org  User-Agent:Bacon/1.0  Cookie:valued-visitor=yes;foo=bar X-Foo:Bar  Referer:http://httpie.org/
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Cookie: valued-visitor=yes;foo=bar
Host: example.org
Referer: http://httpie.org/
User-Agent: Bacon/1.0
X-Foo: Bar

HTTPie默认设置了需要头信息

GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: HTTPie/<version>
Host: <taken-from-URL>

默认的头可以重写也可以清除掉,清除已指定的头(比如一个默认的头),用Header:

$ http httpbin.org/headers Accept: User-Agent:

如果要传空值的头,用Header;

$ http httpbin.org/headers ‘Header;‘

  

 

[译]httpie: CLI, 类似cURL的客户端

标签:

原文地址:http://www.cnblogs.com/iostream7/p/5935386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!