[Translate]httpie:cli, curl-like client

Source: Internet
Author: User
Tags form post http post

5 Request URL

Httpie executes the request, the only information that needs to be specified is the URL. The default scheme, no unexpected, http://, can be ignored, the example.org is also possible.

In addition, a shorthand method like curl is supported. For example: 3000 is http://localhost:3000 if the port is ignored, the default use of 80

$ http:/foo
Get/foo Http/1.1host:localhost
$ http:3000/bar
Get/bar http/1.1host:localhost:3000
$ http:
Get/http/1.1host:localhost

If you need to use query string parameters in the terminal hand, you can use Param=value syntax to stitch the URL parameters without worrying about losing the & delimiter. Also, the special characters in the parameters are automatically removed.

You can use the--default-scheme <URL_SCHEME> option to create shortcuts to other protocols

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

  

6 Request Items

There are several different request types that make it easy to specify HTTP headers, JSON-formatted data, form data, files, and URL parameters. Specify a key-value pair after the URL.

These request items have common characteristics that are part of the actual request, they are only delimited by delimiters,,,,,, and : = have the := == @ [email protected] :[email protected], @ symbol is the path to the specified file

Item Type Description

HTTP Headers

Name:Value

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

URL parameters

name==value

To add a key-value pair as a URL for a query string parameter

The == separator is used.

Data fields

field=value,[email protected]

The Request data field is passed by default as a JSON object, or as form-encoded ( --form, -f ).

Raw JSON Fields

field:=json,field:[email protected]

Sometimes JSON is passed, some field types are, Boolean Number inline Objects t , or an Array ,

e.g., meals:=‘["ham","spam"]‘ or pies:=[1,2,3] (note the quotation marks).

Form File Fields

[email protected]/dir/file

Just used --form, -f的时候有效 .

For example [email protected]~/Pictures/img.png . This form will result in a request for a multipart/form-data .

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

Sometimes it is necessary to use quotation marks to e.g the value. foo=‘bar baz‘

If the field names start with a minus sign (e.g. -fieldname ), they need to be placed in -- 后面以避免冲突  --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"}

Note data fields are not the only way to pass the request data, redirected input can also pass unique data

7 JSON

JSON is a common language for modern Web services and is the implicit type that Httpie uses by default:

If the command line contains data items, they are serialized in JSON format by default. Httpie will automatically set the following headers, which can also be overridden:

Content-type Application/json
Accept Application/json, */*

You can also use--json,-j explicitly set the Accept to Application/json, regardless of whether it is necessary to pass the data (there is a shortcut to set the header, HTTP URL via common header identifier Accept: ' Application/json, */* ' )

In addition, Httpie detects the JSON response, even if the content-type is not correct (such as Text/plain) or is not set.

Example:

$ http PUT example.org Name=john [email protected]
Put/http/1.1accept:application/json, */*accept-encoding:gzip, Deflatecontent-type:application/jsonhost: example.org{    "name": "John",    "email": "[email protected]"}

Non-string fields need to be: = delimiter, you can insert pure JSON into the object. Text and plain JSON files can be inserted by using [email protected] and: [email protected] identifiers, respectively:

$ http PUT api.example.com/person/1     name=john     age:=29 married:=false hobbies:= ' ["http", "pies"] ' \  # Raw JSON    [email protected] \   # Embed text file    bookmarks:[email protected]      # Embed JSON file
PUT/PERSON/1 Http/1.1accept:application/json, */*content-type:application/jsonhost: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",    }}

Passing JSON data in file form is as follows:

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

8 Form8.1 Rule Form
$ http--form POST api.example.org/person/1 name= ' John Smith '
POST/PERSON/1 http/1.1content-type:application/x-www-form-urlencoded; Charset=utf-8name=john+smith

8.2 File Upload Form

If you provide more than one field, the serialized content type is Multipart/form-data

$ http-f POST example.com/jobs name= ' John Smith ' [Email protected]~/documents/cv.pdf

The above request is the same as in the case of the following HTML form submission:

<formenctype= "Multipart/form-data"Method= "POST"Action= "Http://example.com/jobs">    <inputtype= "text"name= "Name" />    <inputtype= "File"name= "CV" /></form>

Note that the @ symbol is used to simulate form fields for file uploads, whereas the [email protected] symbol simply inserts the contents of the file as text

9. HTTP Headers

If you want to set a custom header you can use the Header:value marker

$ http example.org  user-agent:bacon/1.0  'cookie:valued-visitor=yes;foo=bar ' X-foo:bar  referer:http://httpie.org/
Get/http/1.1accept: */*accept-encoding:gzip, Deflatecookie:valued-visitor=yes;foo=barhost:example.orgreferer: Http://httpie.org/User-Agent:Bacon/1.0X-Foo:Bar

HTTPie The required header information is set by default

Get/http/1.1accept: */*accept-encoding:gzip, Deflateuser-agent:httpie/<version>host: <taken-from-URL>

The default header can be overridden or cleared, and the specified header (such as a default header) is cleared, using the header:

$ http httpbin.org/headers accept:user-agent:

If you want to pass the empty value of the header, use the header;

$ http httpbin.org/headers ' Header; '

  

[Translate]httpie:cli, curl-like client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.