Installing HTTPIE (HTTP for Humans) on Windows - Great for ASP.NET Web API and RESTful JSON services
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.
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!
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.
About Newsletter
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.
Mike - I wanted people to add it to the SYSTEM environment and not just their PowerShell profile.
It's not got the chic appeal of command line but is useful if you tend to do your debugging in Chrome.
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") | pythondoes the same thing.
Trying to automate a smoke test has lead me to driving IE via automation (selenium), which is pretty painful.
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.)
Installed HTTPie using pip
This is awesome. Scott, thanks for the tip.
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
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
BTW, I believe that it should have been C:\Python32\Tools\Scripts instead of C:\Python32\Scripts.
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
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.
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
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.
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.