在Windows上安装HTTPIE(人类HTTP)-非常适合ASP.NET Web API和RESTful JSON服务

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

最近在RESTful ASP.NET Web API框架上的一篇文章中,我使用curl将JSON发布到HTTP端点:

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很可爱,应该位于c:\ utils文件夹中,更重要的是位于PATH中。 我的Dropbox和所有机器上的PATH中都有一个UTILS文件夹。 每当我找到有用的免安装实用程序时,就会将其放在其中。

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.

Curl很棒,但是它仍然让我感到困惑,以至于我使用得还不够。 命令行开关有点晦涩难懂,这使我无法定期使用它。

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

对于HTTP工作, http: //httpie.org上有一个更好的实用程序,称为HTTPie。 (与IE(Internet Explorer)无关。) 对于一直使用Python的Mac和Linux人士,安装简便,您只需

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.

对于不使用Python的Windows人士来说,安装起来有点困难,但这是值得的,我建议您花点时间进行设置。 您会想知道没有它的生活。

安装HTTPIE (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.

首先,下载Python 。 我获得了x86版本的Python 3.2.3,因为它是最新版本,我认为我不需要x64版本。

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

然后,我在路径中添加了c:\ python32和c:\ python32 \ scripts。 我通过点击WinKey + Break,然后选择Advanced,再选择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.

其次,下载CURL 。 是的,我意识到这一点很讽刺,但是它仍然是非常有用的工具。 我下载了7.27二进制SSL Win32版本,将其解除阻止,解压缩并将其放在C:\ UTILS中,以便它自动出现在我的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.

第三,从管理员命令提示符运行。 再次注意,它需要PATH中的curl.exe和python.exe才能按原样运行。 这应该没有问题地运行。

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”结尾。

Pip is a Python package manager.

Pip是Python软件包管理器。

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.

我建议您安装HTTPie的开发边缘版本,而不仅仅是“ pip install httpie”,因为开发人员正在积极解决Windows问题,最近才帮助我解决了一个问题。

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.exe,python.exe和python脚本都在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

运行HTTPie (Running HTTPie)

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

如果您可以从命令行运行“ http”并获取以下输出,您就会知道它有效:

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.

这就是乐趣发生的地方。 语法非常直观。 在这里,我将一些JSON发布到将回显它的终结点。

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!

就像从命令行使用HTTP本身一样。 最好的部分是它将对字符串使用name = value,对于非字符串将使用name:= value并将其转换为JSON!

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

HTTPie支持任何HTTP Verb,FORM数据,原始JSON和许多其他功能。 这是另一个例子:

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!

https://github.com/jkbr/httpie/上有更多示例,我鼓励您检查一下。 我将为您提供一个可爱的PowerShell屏幕截图,该屏幕截图显示HTTPie还在命令行中突出显示了语法!

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.

太棒了希望在我所有的Web API和JSON演示中都能看到此工具。 去获取它并在GitHub上加注星标

翻译自: https://www.hanselman.com/blog/installing-httpie-http-for-humans-on-windows-great-for-aspnet-web-api-and-restful-json-services

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值