Scott Hanselman

Installing HTTPIE (HTTP for Humans) on Windows - Great for ASP.NET Web API and RESTful JSON services

August 17, '12 Comments [38] Posted in ASP.NET | Open Source | Web Services
Sponsored By

Recently in on a post on the RESTful ASP.NET Web API framework I used curl to post JSON to an HTTP endpoint:

curl -X POST -H "Content-Type: application/json" -d "{ Name: 'Scott Guthrie', Age: 67}"

Curl is lovely and should be in your c:\utils folder and more importantly in your PATH. I have a UTILS folder in my Dropbox and in the PATH on all my machines. Whenever I find a useful no-install utility I put it in there.

Curl is great but it's still confusing enough to me that I don't use it enough. It's slightly obscure command-line switches are keeping me from using it on a regular basis.

For HTTP work there is a better utility called HTTPie at http://httpie.org. (It has nothing to do with IE (Internet Explorer)). For Mac and Linux folks who use Python all the time, it's easy to install, you just

pip install -U httpie

For Windows folks who don't use Python it's a little harder to install, but it's worth it and I recommend you take a moment and set it up. You'll wonder how you lived without it.

Installation of HTTPIE

First, go download Python. I got the x86 version of Python 3.2.3 cause it was the latest and I didn't think I needed the x64 one.

I then added c:\python32 and c:\python32\scripts to my path. I do this by hitting WinKey+Break, then Advanced, then Environment.

Add Python and Python/Scripts to your PATH

Second, download CURL. Yes, I realize the irony, but it's still a VERY useful tool. I downloaded the 7.27 binary SSL Win32 version, unblocked it, unzipped it and put it in C:\UTILS so it was automatically in my PATH.

Third, run this from an Administrator command prompt. Note again that it needs both curl.exe and python.exe in the PATH to run as it is. This should run without incident.

curl http://python-distribute.org/distribute_setup.py | python

Then run

curl -k https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

This should end with "successfully installed pip."

Pip is a Python package manager.

Finally, run

pip install -U https://github.com/jkbr/httpie/tarball/master

I'm recommending you install the development edge build of HTTPie rather than just "pip install httpie" as the developer is actively fixing Windows issues and just recently helped me with one.

So, to sum up what you need to run, in four lines, assuming curl.exe, python.exe and python scripts are all in your PATH.

curl http://python-distribute.org/distribute_setup.py | python
curl -k https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
pip install -U https://github.com/jkbr/httpie/tarball/master

Running HTTPie

You'll know it works if you can run "http" from the command line and get this output:

C:\Users\scottha\Desktop> http
usage: http-script.py [--help] [--version] [--json | --form] [--output FILE]
[--pretty | --colors | --format | --ugly]
[--print OUTPUT_OPTIONS | --verbose | --headers | --body]
[--style STYLE] [--stream] [--check-status]
[--auth USER:PASS] [--auth-type {basic,digest}]
[--verify VERIFY] [--proxy PROXY] [--allow-redirects]
[--timeout SECONDS] [--traceback] [--debug]
[METHOD] URL [ITEM [ITEM ...]]
http-script.py: error: too few arguments

Here's where the fun happens. The syntax is VERY intuitive. Here I post some JSON to an endpoint that will echo it back.

C:\> http POST http://localhost:50231/api/Contact name=scott age:=100
HTTP/1.1 200 OK
Content-Length: 26
Content-Type: application/json; charset=utf-8
Date: Fri, 17 Aug 2012 21:59:51 GMT
Server: Microsoft-HTTPAPI/2.0

{
"age": 100,
"name": "scott"
}

It's just like using HTTP itself, except from the command line. The best part is that it will take name=value for strings and name:=value for non-strings and turn it into JSON!

HTTPie supports any HTTP Verb, FORM data, raw JSON, and lots of other features. Here's another example:

C:\>http PUT api.example.com/person/1 name=John age:=29 married:=false hobbies:='["http", "pies"]'
PUT /person/1 HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: api.example.com
User-Agent: HTTPie/0.2.7dev

{
"age": 29,
"hobbies": [
"http",
"pies"
],
"married": false,
"name": "John"
}

There's lots more examples here https://github.com/jkbr/httpie/ and I encourage you to check it out. I'll leave you with a lovely PowerShell screenshot showing that HTTPie also does syntax highlighting at the command line!

HTTPie is HTTP for Humans and Syntax Highlights as well

Awesome. Expect to see this tool in all my Web API and JSON demos. Go get it and star it at GitHub.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Friday, August 17, 2012 10:52:07 PM UTC
That's pretty cool. Now do it in PowerShell so that it returns a rich object with metadata and json response.
Friday, August 17, 2012 10:57:54 PM UTC
Hey, this looks like a great tool and a great tutorial on setting up cUrl, Python via command line. But why not set the environment path variables via the command line as well? Here's some ways to do it: http://stackoverflow.com/questions/714877/setting-windows-powershell-path-variable
Friday, August 17, 2012 11:14:48 PM UTC
Scott, doesn't Python on Windows come with easy install baked in? If so, you can "easy_install pip" to get pip. You can also probably "easy_install httpie" directly (but not the dev edge version).
Friday, August 17, 2012 11:36:09 PM UTC
I'm playing with building a PowerShell version :)
Saturday, August 18, 2012 12:04:06 AM UTC
Check out Fiddler's Composer tab for similar functionality. It doesn't have the cool pseudo-JSON syntax, but I find that copy-paste-modify from previous responses is quite easy and reduces errors.

John Peterson puts this technique to great use in his aspConf Intro to Web API session. Specifically, check out 40:25-43:00 where he GETs a resource, copies the JSON response, tweaks it and PUTs it, and then GETs it to show that the changes worked. Earlier in the session (32:20-34:20) he also shows tweaking the Content-Type header to toggle between XML, JSON, or whatever.

Anyway, while HTTPIE looks pretty cool, I think Fiddler's Composer demos better, will encounter lower Microsoft cultural resistance, and is nicely integrated into other Fiddler features you already know and love.
Saturday, August 18, 2012 12:14:18 AM UTC
Whoops, I see you're already familiar with using Fiddler's Composer to post JSON here! Regardless, I hope this helps someone discover another one of Fiddler's under-utilized cool features.
Saturday, August 18, 2012 12:26:39 AM UTC
Haha, and then I just now watched your 6-minute ASP.NET Web API video and you used Fiddler's Composer to do the exact same things as John Peterson (Accept/Content-Type and reposting modified JSON), but in a more compressed "Hanselminutes" manner! Awesome! I recommend folks check it out, from 3:30 until the end.
Saturday, August 18, 2012 1:33:30 AM UTC
Steve - easy_install is on the way out and pip is recommended going forward so I thought I'd encourage folks to use the right stuff.

Mike - I wanted people to add it to the SYSTEM environment and not just their PowerShell profile.
Saturday, August 18, 2012 7:09:35 AM UTC
I you're using Chrome, there's a pretty good plugin/app/thing from Play Store (no idea what the correct nomenclature is) called Advanced REST Client that let's you do similar stuff.

It's not got the chic appeal of command line but is useful if you tend to do your debugging in Chrome.
Saturday, August 18, 2012 5:14:31 PM UTC
I used the Cygwin installer to get curl and python. This tool is awesome. Thanks for sharing!
Saturday, August 18, 2012 5:48:27 PM UTC
Now with the Invoke-WebRequest, and Invoke-RestMethod in PS v.3 we will be able to replicate most of curls functionality. Get busy Doug Finke, : )
Sunday, August 19, 2012 2:29:53 AM UTC
Scott, my point was to use "easy_install" to get pip. It seems like the easiest way.
Sunday, August 19, 2012 9:33:06 AM UTC
What else is in your C:\utils, Scott?
Sunday, August 19, 2012 1:56:55 PM UTC
Install Pscx to get...

Add-PathVariable C:\Python27 -Target Machine -Target Process


...along with many other goodies.

Personally, I just went through a fresh install of Windows 8 and had to update everything, and I do all this from the commandline. I don't use curl anymore, since
(New-Object Net.WebClient).DownloadString("http://python-distribute.org/distribute_setup.py") | python
does the same thing.
Monday, August 20, 2012 7:12:14 AM UTC
We use webforms. We're pretty much stuffed for either curl or httpie correct? (At least with respect to put/post requests).

Trying to automate a smoke test has lead me to driving IE via automation (selenium), which is pretty painful.
Josh
Monday, August 20, 2012 1:47:29 PM UTC
Yes, pip is in the process of replacing easy_install, but the accepted way to bootstrap pip is still to easy_install pip (then never use easy_install again).

Another good alternative on Windows would be to use Chocalatey:

http://chocolatey.org/packages/pip

Certainly the combined installation of both Chocalatey and then pip from the command line is a shorter, subtler two line dance:

C:\> @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://bit.ly/psChocInstall'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
C:\> cinst pip

Note that the cinst pip would also install Python as needed.

(Also, I still find it fascinating the way Chocalatey uses powershell as its CURL and script engine all in one CMD command line.)
Monday, August 20, 2012 2:37:22 PM UTC
Installed pip using Chocolatey (I recommend this method)

Installed HTTPie using pip

This is awesome. Scott, thanks for the tip.
Monday, August 20, 2012 4:31:19 PM UTC
@Josh - Yeah, I'm suffering the same issues - it looked like the "session" option might work, but no luck with forms-based authentication. Or at least I couldn't get --session to work with --form POST. If someone does know of a way, that'd be awesome....
Monday, August 20, 2012 7:55:15 PM UTC
Can you please post a list of all of the tools in your C:\Utils folder? We're all very interested!
Monday, August 20, 2012 8:03:09 PM UTC
Any suggestions on how to get this to work behind an NTLM proxy? I was able to get curl to work, by setting a proxy in the environment setting and passing my username and password. However, running the python script failed at create connection since the proxy server refused it.
Tuesday, August 21, 2012 11:39:38 AM UTC
Another great alternative for editing your Env Vars is a free Rapid Environment Editor at http://www.rapidee.com/. I sure have that in my c:\utils.
Wednesday, August 22, 2012 1:07:42 AM UTC
Hey Scott,

Your Curl link seems a bit off.

Wednesday, August 22, 2012 11:34:32 AM UTC
You can also just, you know, use a browser to download distribute_install.py and get-pip.py :) (just dump those links into an address bar). Didn't know easy_install will install pip, though, that will save me some time in the future!
Thursday, August 23, 2012 5:08:01 AM UTC
Scott Frost, start here: http://hanselman.com/tools
Thursday, August 23, 2012 6:53:02 AM UTC
I do prefer to use Poster, which is a Firefox plugin and makes creating HTTP request easy. Fiddler also rocks.
Thursday, August 23, 2012 7:24:17 AM UTC
OK, no install one-liners that work on a fresh windows install:

set PATH=%PATH%;<python-dir>;<python-dir>\Scripts
python -c "from urllib.request import urlopen;exec(urlopen('http://python-distribute.org/distribute_setup.py').read())"
easy_install pip
pip install -U https://github.com/jkbr/httpie/tarball/master
Thursday, August 23, 2012 7:41:48 PM UTC
I prefer Postman for Chrome. I like it much better than trying to write JSON on a command line.
Friday, August 24, 2012 12:40:42 PM UTC
Hi Scott,

This is a really great article! That is all. :)
Saturday, August 25, 2012 6:29:05 AM UTC
I dont want to install python on my machine - as this is so Un-Windows :-)
But I also would like to use that tool.
Therefore I started a .net port of this tool on github

https://github.com/ChrisOMetz/HTTPie.net
Tuesday, August 28, 2012 10:39:19 AM UTC
Nice post Sir.

BTW, I believe that it should have been C:\Python32\Tools\Scripts instead of C:\Python32\Scripts.
Tuesday, August 28, 2012 10:43:38 AM UTC
Ah, sorry. You were right. I should stay the same as C:\Python32\Scripts. I realized that after installing pip.
Thursday, September 27, 2012 3:42:02 PM UTC
@Robert I managed to get this working behind our proxy, as detailed below. I don't whether this approach can be easily adapted to cover your particular case, but even if not it may be of some use to others.

To start with, I downloaded the first python script using
curl -x [proxy ip] http://python-distribute.org/distribute_setup.py > dist_setup.py

and added the following lines to the beginning of the download_setuptools() method before running the script using python dist_setup.py:

import urllib.request as req
proxyip = 'xxx.xxx.xxx.xxx'
proxy = req.ProxyHandler({'http': proxyip)
opener = req.build_opener(proxy, req.HTTPHandler)
req.install_opener(opener)


I then downloaded the second script using
curl -x [proxy ip] -k https://raw.github.com/pypa/pip/master/contrib/get-pip.py > get-pip.py
and similarly inserted the same lines above to the beginning of the unpack() method at the end of the script. Running the modified script using python get-pip.py successfully installed pip.

Finally, I provided the --proxy parameter to pip to successfully complete the install of httpie:

pip install --proxy [proxyip:proxyport] -U https://github.com/jkbr/httpie/tarball/master
Henry Buckley
Saturday, October 06, 2012 8:48:07 PM UTC
Thanks for the great article.

I've been using Chrome's Advanced Web Client with much success but I find it's lacking a good request library to organise those large numbers of sample HTTP requests what are a typical by-product of a RESTful based dev project. HTTPIE gives me much more flexibilty in this regard.

By the way, the Pip installation failed to work for me using Python 3.X (Windows 8). Not to worry as I sailed through it after a quick install of Python 2.2.7. I would recommend this well written tutorial for those who run into similar installation difficulties.
Finbar Leahy
Thursday, November 29, 2012 7:04:55 PM UTC
Anyone get this working through a proxy? All web request fire me a 407.
Reuben
Monday, December 03, 2012 2:25:12 AM UTC
Scott, I still cant get why are you using curl if you have fiddeler installed?
Frost
Monday, December 17, 2012 5:35:08 PM UTC
Scott,

How did you get the highlighting to work in your powershell console? When I run *http* in a PW console, I see ASCII gibberish like below:

PS C:\> http GET http://www.google.com --style=fruity
←[34mHTTP←[39;49;00m/←[39;49;00m←[34m1.1←[39;49;00m ←[39;49;00m←[34m200←[39;49;00m ←[39;49;00m←[36mOK←[39;49;00m
←[36mCache-Control←[39;49;00m:←[39;49;00m ←[39;49;00mno-cache←[39;49;00m
←[36mDate←[39;49;00m:←[39;49;00m ←[39;49;00mMon, 17 Dec 2012 17:29:06 GMT←[39;49;00m
←[36mPragma←[39;49;00m:←[39;49;00m ←[39;49;00mno-cache←[39;49;00m
←[36mContent-Length←[39;49;00m:←[39;49;00m ←[39;49;00m3161←[39;49;00m
←[36mContent-Type←[39;49;00m:←[39;49;00m ←[39;49;00mtext/html; charset=UTF-8←[39;49;00m
←[36mExpires←[39;49;00m:←[39;49;00m ←[39;49;00mThu, 01 Jan 1970 00:00:00 GMT←[39;49;00m
←[36mSet-Cookie←[39;49;00m:←[39;49;00m ←[39;49;00mADMINCONSOLESESSION=yQqDQPWCr7w9wn5dfdtqLfCs3Jx9J4DHT1YKvQDprYJrGx5LvB
JV!-1034303106; path=/←[39;49;00m
←[36mContent-Language←[39;49;00m:←[39;49;00m ←[39;49;00men-US←[39;49;00m
←[36mX-Powered-By←[39;49;00m:←[39;49;00m ←[39;49;00mServlet/2.5 JSP/2.1←[39;49;00m

I'm running on:

Win XP Pro Version 2002 SP3
robin
Monday, January 21, 2013 8:19:29 PM UTC
for lazy people.
I used. I had to use a rest api (mandrill.com) and I stumbled upon this article.

My 50 cents: I used Fiddler + plugin to generate code c# form request.
Wednesday, April 24, 2013 6:21:31 AM UTC
Anyone from you tried portable version of python and httpie? Asking because my colleague wanted to use httpie and ended at last step (some error, don't remember). So I decided to install httpie on my computer and commit to svn so everybody in our team can use it without installation.

Didn't succeeded, because easy install writes full absolute paths to python script. One quick workaround is to remove the line with absolute path, but imo it is not bulletproof generaly.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.