Science and technology

Getting began with HTTPie for API testing

HTTPie is a delightfully simple to make use of and straightforward to improve HTTP shopper. Pronounced “aitch-tee-tee-pie” and run as http, it’s a command-line software written in Python to entry the online.

Since this how-to is about an HTTP shopper, you want an HTTP server to attempt it out; on this case, httpbin.org, a easy, open supply HTTP request-and-response service. The httpbin.org web site is a strong solution to check to check internet API purchasers and punctiliously handle and present particulars in requests and responses, however for now we are going to concentrate on the ability of HTTPie.

An different to Wget and cURL

You may need heard of the venerable Wget or the marginally newer cURL instruments that help you entry the online from the command line. They have been written to entry web sites, whereas HTTPie is for accessing internet APIs.

Website requests are designed to be between a pc and an finish consumer who’s studying and responding to what they see. This does not rely a lot on structured responses. However, API requests make structured calls between two computer systems. The human is just not a part of the image, and the parameters of a command-line software like HTTPie deal with this successfully.

Install HTTPie

There are a number of methods to put in HTTPie. You can in all probability get it as a bundle in your bundle supervisor, whether or not you utilize brew, apt, yum, or dnf. However, in case you have configured virtualenvwrapper, you may personal your personal set up:

$ mkvirtualenv httpie
...
(httpie) $ pip set up httpie
...
(httpie) $ deactivate
$ alias http=~/.virtualenvs/httpie/bin/http
$ http -b GET https://httpbin.org/get

By aliasing http on to the command contained in the digital atmosphere, you may run it even when the digital atmosphere is just not lively. You can put the alias command in .bash_profile or .bashrc so you may improve HTTPie with the command:

$ ~/.virtualenvs/httpie/bin/pip set up -U pip

Query an internet site with HTTPie

HTTPie can simplify querying and testing an API. One possibility for operating it, -b (also referred to as –body), was used above. Without it, HTTPie will print your complete response, together with the headers, by default:

$ http GET https://httpbin.org/get
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 177
Content-Type: software/json
Date: Fri, 09 Aug 2019 20:19:47 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

This is essential when debugging an API service as a result of numerous info is distributed within the headers. For instance, it’s usually vital to see which cookies are being despatched. Httpbin.org offers choices to set cookies (for testing functions) by way of the URL path. The following units a cookie titled opensource to the worth superior:

$ http GET https://httpbin.org/cookies/set/opensource/superior
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 223
Content-Type: textual content/html; charset=utf-eight
Date: Fri, 09 Aug 2019 20:22:39 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=superior; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You needs to be redirected robotically to focus on URL:
<a href="/cookies">/cookies</a>.  If not click on the hyperlink.

Notice the Set-Cookie: opensource=superior; Path=/ header. This exhibits the cookie you anticipated to be set is about accurately and with a / path. Also discover that, regardless that you bought a 302 redirect, http didn’t observe it. If you wish to observe redirects, you’ll want to ask for it explicitly with the –follow flag:

$ http --follow GET https://httpbin.org/cookies/set/opensource/superior
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 66
Content-Type: software/json
Date: Sat, 10 Aug 2019 01:33:34 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

But now you can not see the unique Set-Cookie header. In order to see intermediate replies, you’ll want to use –all:

$ http --headers --all --follow
GET https://httpbin.org/cookies/set/opensource/superior
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: textual content/html; charset=utf-eight
Date: Sat, 10 Aug 2019 01:38:40 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=superior; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 223
Connection: keep-alive

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: software/json
Date: Sat, 10 Aug 2019 01:38:41 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 66
Connection: keep-alive

Printing the physique is uninteresting since you are largely within the cookies. If you wish to see the headers from the intermediate request however the physique from the ultimate request, you are able to do that with:

$ http --print hb --history-print h --all --follow
GET https://httpbin.org/cookies/set/opensource/superior
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: textual content/html; charset=utf-eight
Date: Sat, 10 Aug 2019 01:40:56 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=superior; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 223
Connection: keep-alive

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: software/json
Date: Sat, 10 Aug 2019 01:40:56 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 66
Connection: keep-alive

You can management precisely what’s being printed with –print and override what’s printed for intermediate requests with –history-print.

Download binary information with HTTPie

Sometimes the physique is non-textual and must be despatched to a file that may be opened by a distinct software:

$ http GET https://httpbin.org/picture/jpeg
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 35588
Content-Type: picture/jpeg
Date: Fri, 09 Aug 2019 20:25:49 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

+-----------------------------------------+
| NOTE: binary information not proven in terminal |
+-----------------------------------------+

To get the fitting picture, you’ll want to reserve it to a file:

$ http --download GET https://httpbin.org/picture/jpeg
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 35588
Content-Type: picture/jpeg
Date: Fri, 09 Aug 2019 20:28:13 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

Downloading 34.75 kB to "jpeg.jpe"
Done. 34.75 kB in zero.00068s (50.05 MB/s)

Try it! The image is lovely.

Sending customized requests with HTTPie

You may ship particular headers. This is beneficial for customized internet APIs that require a non-standard header:

$ http GET https://httpbin.org/headers X-Open-Source-Com:Awesome

  "headers":

Finally, if you wish to ship JSON fields (though it’s potential to specify precise content material), for a lot of less-nested inputs, you should use a shortcut:

$ http --body PUT https://httpbin.org/something open-source=superior creator=moshez

  "args": ,
  "data": ""open-source": "superior", "creator": "moshez"",
  "files": ,
  "form": ,
  "headers":
    "Accept": "application/json, */*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "46",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "HTTPie/1.0.2"
  ,
  "json": ,
  "method": "PUT",
  "origin": "73.162.254.113, 73.162.254.113",
  "url": "https://httpbin.org/anything"

The subsequent time you’re debugging an internet API, whether or not your personal or another person’s, put down your cURL and attain for HTTPie, the command-line shopper for internet APIs.

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top