Saturday, August 18, 2012

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

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.

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.


© 2012 Scott Hanselman. All rights reserved.


DIGITAL JUICE

No comments:

Post a Comment

Thank's!