Code Monkey home page Code Monkey logo

rural's Introduction

crates.io Build Status

rural - User-friendly command-line HTTP tool in Rust

Why rural?

Most developers tend to use curl when they need to make HTTP requests from the command line. In terms of speed and breadth of features, curl pretty much has it all. However, in terms of the command-line API, curl can sometimes leave something to be desired. For example, the -G or --get flag can be used to make a GET request, but there is no --post option, and -P is used to instruct the server to connect to an open FTP port instead of sending the reponse back directly.

One alternative to using curl for command-line HTTP requests is HTTPie. HTTPie has an excellent command-line API, including some superb syntatic sugar for various parameter types and headers. However, being written in Python, HTTPie is quite a bit slower than curl.

Rural aims to provide the ease-of-use of HTTPie at a speed comparable to that of curl. To that end, rural purposely implements a similar API to HTTPie, including the syntatic sugar for parameters and headers.

Status

Rural currently implements most of the common features needed for making HTTP requests in the command line. However, many less common features have not been implemented. (A brief look at man curl shows just how much functionality there is to include). If there's a feature you'd like to be implemented, feel free to open an issue, or even better, a pull request! Contributions are always quite welcome.

To date, no specific optimization has been done on rural. From a set of extremely unscientific tests, rural currently runs at the same order of magnitude as curl in terms of speed (which is an order of magnitude faster than HTTPie).

Installation

If you don't already have Rust installed, you can get the Rust toolchain installer here.

Once Rust is installed, run cargo install rural. Make sure you add $HOME/.cargo/bin to your PATH if you instructed rustup not to do so itself.

Usage

Method types

Rural currently supports making GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH requests. To make a request, invoke rural with the request method (in lowercase) and the URL:

rural get http://example.com
rural post http://example.com
rural put http://example.com
rural delete http://example.com
rural head http://example.com
rural options http://example.com
rural patch http://example.com

HTTPS

Rural requires OpenSSL to make HTTPS requests:

rural get https://example.com

Output

Sections

To see the response headers instead of the body, use the --headers flag (-d for short):

rural --headers get http://example.com
rural -d get http://example.com

Note that whenever the headers are printed out, the HTTP version number and response status code are printed by default. To suppress this extra output and just get the headers, add the --suppress-info flag as well (-s for short):

rural --headers --suppress-info get http://example.com
rural -ds get http://example.com

To print both the reponse headers and the body, use the --both flag (-b for short):

rural --both get http://example.com
rural -b get http://example.com

--suppress-info works for --both as well:

rural --both --suppress-info get http://example.com
rural -bs get http://example.com

Output file

To save the response body to a file, use the --out argument with the desired output file name (-o for short):

rural get http://example.com --out output.json
rural get http://example.com -o output.html

Colors

By default, rural will colorize the response headers, the HTTP info string, and any JSON in the response body. To suppress this, use the --no-color flag (-n for short):

rural --no-color http://example.com
rural -n http://example.com

NOTE: The library used to colorize the JSON uses ANSI color escape sequences, which will not work correctly on Windows. Because of this, rural disables colorized output on Windows.

Parameters

Rural supports supplying GET parameters in the querystring or by using the syntax key==value:

rural get 'http://example.com?bass=john&drums=keith'
rural get http://example.com bass==john drums==keith

To supply body parameters, use the syntax key=value:

rural post http://example.com bass=john drums=keith

Rural defaults to sending body parameters as JSON. To instead send them as form parameters, use --form (-f for short):

rural --form post http://example.com bass=john drums=keith
rural -f post http://example.com bass=john drums=keith

Body parameters can also be sent using literal JSON rather than plaintext. To do this, use the syntax key:=value (note that JSON values will generally have to be wrapped in quotes due to how most shells interpret some of the characters, as shown below):

rural post http://example.com who:='{ "bass": "john", "drums": "keith", "others": ["pete", "roger"] }'

Note that body parameters can also be specified for other types of requests besides POST:

rural put http://example.com bass=john
rural delete http://example.com drums=keith

Headers

HTTP headers (either standard or custom) can be provided using the syntax name:value:

rural get http://example.com bass:john drums:keith

License

Rural is licensed under the MIT LICENSE.

rural's People

Contributors

saghm avatar leiserfg avatar

Stargazers

Dominik Wilkowski avatar Andy Aylward avatar Andreas Mausch avatar Ivan Prisyazhnyy avatar Szymon Kozioł avatar  avatar Wandalen avatar Robert Jack Will avatar Vinay Aggarwal avatar  avatar Dmitry Kozlovtsev avatar Max avatar  avatar Valérie ROUX avatar Gaurav Kanoongo avatar 许杰友 Jieyou Xu (Joe) avatar Rob Ede avatar  avatar Wei Tan avatar David Weis avatar Steven Barragán Naranjo avatar Caleb Jasik avatar SM avatar Jonathan Ling avatar Waldir Pimenta avatar Hawken Rives avatar  avatar Akihito Morita avatar Cheng JIANG avatar Weiwen Zhang avatar Mitch Tishmack avatar Matthew Graham avatar Jake McGinty avatar Geordon Worley avatar Andy Cobaugh avatar Yubao Liu avatar Barlog M. avatar Alin Panaitiu avatar Casey Marshall avatar Darin Gordon avatar Duncan Lock avatar Steven  BOEHM avatar Jakub Neander avatar Jakub Pastuszek avatar Stephen Muss avatar Paulo Duarte avatar whoami avatar Jürgen Hermann avatar Hiten Sharma avatar Omer Katz avatar Jakub Roztocil avatar Jacob Budin avatar  avatar Siliang Cao avatar Mike Lamb avatar Wesley Moore avatar Masashi Fujita avatar Ruben Blanco avatar Miki Oracle avatar Maximilian Goisser avatar Davorin Šego avatar

Watchers

James Cloos avatar  avatar

rural's Issues

hey RustNYCer :)

Hey Saghm! This project is a great idea! Rust command line utilities for the win.

Someone recently ported httpbin.org to Rust. You might for this useful for rural testing: https://github.com/svartalf/actix-httpbin

I noticed that rural isn't playing nicely with jq. For instance, this works with HTTPie whereas with rural it doesn't: http GET "https://httpbin.org/json" | jq

This produces an error: rural get "https://httpbin.org/json" | jq
parse error: Invalid numeric literal at line 2, column 4

-Darin

blogspot sez "Your client has issued a malformed or illegal request"

$ rural get http://steved-imaginaryreal.blogspot.co.za
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>

Allow omission of "get" and "http://"

With cURL you can simply type curl google.com, but with rural you need to type rural get http://google.com. That's more than 10 characters longer. It would be more convenient if rural would infer "get" and "http://" by default.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.