Giter VIP home page Giter VIP logo

treq's Introduction

TReq

GitHub Workflow Status GitHub repo size

A Terminal Request HTTP Client. TReq is a user-friendly Command Line Interface (CLI) HTTP client, designed to be simple and a complete CLI tool to interact with APIs. With options to save and edit frequently used requests with simple commands to run them again later. Imagine a workflow like GUI tools for HTTP requests in terminal.

demo

Features

  • Made to APIs and REST: TReq is tailored for working with APIs, REST, and JSON with minimal effort.
  • HTTPie based: The CLI interface is entirely based on HTTPie, and seeks to implement and extend its main features (a superset of HTTPie's).
  • Persistent Request Storage: Save and edit frequently used requests with simple commands to run them again later. Imagine a workflow like GUI tools for HTTP requests in terminal.
  • Pretty Outputs: The UX is relevant in a CLI.

Examples

Basic requests

treq GET example.com/users/id?name=John
treq POST example.com

POST with custom header and json payload

treq POST example.com X-API-Token:123 name=John food=pizza

Submit and saving the request locally as "main-endpoint" with --save-as flag

treq POST example.com name="John Doe" --save-as main-endpoint

Executing saved request with run command

treq run main-endpoint

Executing it adding more data

treq run main-endpoint email='[email protected]' job=dev

A pratical usage...

# Create a user and save the request for make it again later
treq POST api.com/user name=John job=dev friends:='["Bob", "Jane"]' birth-year:=1990 --save-as create-user

# Make the same request for create a user "Jane"
treq run create-user name=Jane birth-year:=2001

# Editing saved request
treq edit birth-year:=2002 --method PATCH

Installation

Ubuntu / Debian based

Download the latest .deb package from the last release page. Open your terminal and navigate to the directory where the downloaded .deb file is located. Install TReq using the following command:

sudo dpkg -i treq-x.x.x_amd64.deb

Alternatively, you can try:

sudo apt install ./treq-x.x.x_amd64.deb

Arch / Manjaro

If you're using Arch Linux, you can install TReq from the AUR using an AUR helper such as yay:

yay -S treq-bin

Cargo

For any OS, the best way to install TReq is using cargo.

Install cargo using rustup and then...

cargo install treq

Linux generic

TReq's binary is statically linked and has no dependencies, making it compatible with most major Linux distributions. To install, download the binary from the latest release page and place it in your PATH directory.

Using Curl

curl -fLo /usr/local/bin/treq --create-dirs https://github.com/talis-fb/TReq/releases/latest/download/treq.bin
chmod +x /usr/local/bin/treq

Using wget

wget -O /usr/local/bin/treq https://github.com/talis-fb/TReq/releases/latest/download/treq.bin
chmod +x /usr/local/bin/treq

Windows

Download the latest .exe file at last release page. Place the downloaded .exe file in a directory included in your system's PATH, or add the directory containing the .exe to your PATH.

Usage

For more detailed information on commands and options, refer to the built-in help:

treq --help

TReq uses HTTPie's request-item syntax to set headers, request body, query string, etc.

  • =/:= for setting the request body's JSON or form fields (= for strings and := for other JSON types).
  • == for adding query strings.
  • : for adding or removing headers e.g connection:keep-alive or connection:.

Body, header e params manipulation

# POST with JSON payload => { "language": "Rust", "food": "pizza" }
treq POST example.com language=Rust food=pizza


# POST with custom Header => { Content-Type: application/json }
treq POST example.com Content-Type:application/json


# Define query params at url 
#  (these two below are equivalent)
treq example.com?name=John&job=dev
treq example.com name==John job==dev

More complex requests

# POST with JSON payload 
#  => { 
#    "friends": ["John", "Jane"], 
#    "job": "dev",
#    food": "pizza" 
#  }

#  (these three below are equivalent)
treq POST example.com?sort=true --raw '{ "friends": ["John", "Jane"] }' job=dev food=pizza

treq POST example.com?sort=true --raw friends:='["John", "Jane"]' job=dev food=pizza

treq POST example.com sort==true --raw friends:='["John", "Jane"]' job=dev food=pizza

Localhost alias

When defining urls with localhost, you can use the alias :{PORT}/{ROUTES} instead of complete url.

For example, each pair of the commands below are equivalents...

treq GET localhost:8000
treq GET :8000


treq GET localhost:80/users 
treq GET :80/users


treq run my-request --url localhost:9000
treq run my-request --url :9000

Contributing

Contributions and feature requests are welcome! Feel free to submit issues or pull requests on our GitHub repository.

Upcoming features

treq's People

Contributors

summergram avatar talis-fb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

treq's Issues

Add support for HTTP/2

The system does not support requesting with HTTP/2. The idea is provide this support and adding as feedback in output when using the HTTP/2.

It's also wished some flag as --http2 for global requests commands, to force it to use HTTP2 only.

Come from: #18

Syntax Highlighting with JSON colors for payload outputs

To enhance the visual clarity of the output, it is proposed to implement syntax highlighting with JSON colors for both the payload and headers in the treq command. This improvement aims to provide a more well-formatted and visually appealing output, making it easier for users to interpret and analyze the information.

The current output of body....
image

The wished formatted body... Colorized and pretty to eyes

image

Add `--verbose` or `-v` flags

Beyond just basic response information, all data of request should to appear.

This task depend #22, because this will execute both command with --offline flag and normal command without it.

Add nested JSON fields for `=` and `:=` operators

The objective is to replicate this feature in HTTPie (https://httpie.io/docs/cli/nested-json).

  people[name]=Peter  \
  people[about][job]=dev \
  people[about][country]=Brazil \
  people[about][age]:=27 \
  people[skills]:=["Rust", "Python"]
---
{
    "people": {
        "name": "Peter",
        "about": {
            "job": "dev",
            "country": "Brazil",
            "age": 27
        },
        "skills": [
            "Rust",
            "Python"
        ]
    }
}

Additional Considerations:

  • Ensure that the introduced operator is clearly documented and explained in the treq documentation.
  • Conduct thorough testing to verify the accurate inference of JSON types and proper integration with the existing treq command.
  • Consider providing informative error messages in case of invalid JSON structures.
  • Note: It does NOT necessary add the building arrays through [] suffix like this FIRST EXAMPLE in httpie doc or this example...
search[keywords][]=APIs \
search[keywords][]=CLI
---------
{
 "search": {
        "keywords": [
            "APIs",
            "CLI"
        ],
    }
}

Only focus in define nested values in this task.

Show response time in loading and response

Currently, when making a request through treq, the user experience during loading is limited to a spinner and the text "loading." To provide a more informative experience, it is proposed to add a timer during loading, indicating the elapsed time since the start of the request.

Additionally, in the response, it is suggested to include the response time next to the status code.
Here a example of ideia. This white baloon would be just text with 1.2 MS

image

Alias for simplifying localhost URLs with `:PORT/` instead `localhost:PORT/`

The objective is to replicate the convenient localhost URL shortcut feature found in HTTPie (https://httpie.io/docs/cli/url-shortcuts-for-localhost). The proposal suggests introducing an alias of the form :PORT/ to streamline localhost URLs.

Instead of requiring users to type treq GET localhost:3000/route, they can now use the shorthand treq GET :3000/route.

Expected Behavior:

  • Implement the :PORT/ alias for localhost URLs.

Possible Solution/Implementation

Currently the Url is stored as a single String. In RequestData entity. To facilitate the implementation of this and potential future solutions, it is recommended to replace this single string with a struct that comprehensively stores all URL information.

Url<'a> {
    protocol: Option<&str>, // The default would be "http"
    host: &str,
    port: Option<u16>,
    path: &str,
    query_params: Vec<(&str, &str)>,
    anchor: Option<&str>,
},

This struct should include a function (returning a Result) to parse a string into it, validating if the parameter can be constructed from this struct. This enhancement will not only accommodate the proposed alias feature but also opens the door for additional URL aliases in the future.

Add `--https` flag for set default protocol as https if not specified

Additional Considerations:

If not exist, in CliInput add a bool field for this and use it for implementation.

There are two possible ways to do that then:

  • Add a input validator. The validator should check the https field and updating the Url string for adding this protocol (if not yet).
  • In parse_inputs_to_request_data you can add in final of function a step for check this field in CliInput and set the protocol in RequestData if it is None.

Add Redirect Input to receive raw payload request body

The objective is to replicate this feature in HTTPie: https://httpie.io/docs/cli/redirected-input

Accept user for passing request data is through redirected stdin (standard input)—piping.
Receiving the inputs from this method is similar to using --raw flag with the content passed in stdin.

treq PUT api.com/put < files/data.json
echo -n '{"name": "John"}' | http PATCH api.com/patch Auth:None

These three command are equivalent (consider the file.json with content as {"name": "Thales"})

treq POST api.com/post --raw '{"name": "Thales"}'
echo '{"name": "Thales"}' | treq POST api.com/post
treq POST api.com/post < file.json
cat file.json | treq POST api.com/post

Additional Considerations:

It's really important use the same abstraction used by --raw flag. That way is even possible to merge the input from this with request items.

This command

echo '{ "name": "John", "age": 21 }' | treq POST api.com/post job=dev role=admin

would request with body...

{ 
  "name": "John",
  "age": 21,
  "job": "dev",
  "role": "admin"
}

Add `--offline` flag to construct requests and print them in stdout without sending them

The objective is to replicate this feature in HTTPie (https://httpie.io/docs/cli/offline-mode). This will be applied in Basic request subcommand (like treq GET or treq POST) and treq run. When the --offline flag is used, instead build the request and submit it, the app should return the raw request would be made. This is useful when users want to check if the request he is gonna submit is right.

Additional Considerations:

It's necessary add a field in CliInput. Like "request_items" field another field called "request_options". The field should be passed to ViewCommandExecutor of Submit Request. There, should live a switch. It get and build request, and instead submit it, just prints it. Create a Executor just to this new behavior.

Create a "Give a Try" to new users run TReq via containers in Docker

Implement a Docker container setup for the CLI app with the aim of simplifying deployment and enhancing user experience. The goal is to empower users to effortlessly run the app by issuing a single command, such as docker run my-app. The container should offer a seamless and user-friendly shell environment, allowing users to explore and try out the app without any hassle.

Expected Behavior:

  • A proper setup with Containerfile (Dockerfile) and a pipeline for Github CI/CD to deploy container in Docker Hub
  • The container should be small as possible. It would not be great users download 300mb of an Docker image just to test a CLI.
  • Document in README explicitly guiding users on how to effortlessly "give it a try" using this method. Provide a step-by-step guide for a smooth onboarding experience.
  • There are already some setups using Container with "Containerfiles" at tests/ directory in main folder.

Only append new payload field like `key=value` in `run` command.

To replicate...

# 1. This post will send the payload json { "user": "Peter", "passoword" : "123" }
treq POST url.com user=Peter passoword=123 --save-as login

# 2. Running this command will send the same above payload
treq run login

# 3. Running this will send payload => { "passwork": "12345" }
treq run login password=12345

# In 3 step payload SHOULD BE = { "user": "Peter", "passoword" : "12345" }
#  appending the new "password" field and keeping all the fields saved before.

The current behavior when using body definition operator in run or edit command is replace all body already saved with the construct in inputs, instead using already saved body only appending the input fields.

Add Auto Complete for subcommand and flags

Description

To elevate the user experience, the proposal is to introduce an auto-complete feature

At present, the system lacks inherent auto-completion functionality. This addition aims to assist users in seamlessly completing commands, thereby improving their interaction with the software, reducing the likelihood of errors, and enhancing overall efficiency.

Expected Behavior

  • Provide auto-complete for all subcommands and flags.
  • Provide auto-complete for flags like --url and --method.
  • For flags like --method, provide auto-completion with possible unique values. (POST | GET | PUT ....)

Possible Solution/Implementation

Since the project utilizes Clap for managing the CLI interface, the proposed solution involves configuring or extending its features. Auto-completing saved requests can be achieved by reading the names of all files in {DATA_FOLDER}/collections/.

Setup a Basic CD pipeline in GH Actions to new releases

When new tags (releases) are published the pipeline should....

  • Run all tests like CI pipeline already did
  • Compile TReq binary, using musl target for create a binary with static linked deps
  • Generate a .deb
  • Insert these files (binary and .deb) in release assets

BUG: Invalid non-string value like `key:={"name":]` are parsed

Invalid non-string values like key:={"name":] are parsed for normal string values, because they match in regex of body-value

^(?<key>[ -~]+)=(?<value>[ -~]+)$

The problem is here.... As the parsers_request_items::non_string_body_value return a None (as it not parse it), the next function accept.
image

Add file based operator `@` to insert value from files to request items

The objective is to replicate this feature in HTTPie (https://httpie.io/docs/cli/file-based-separators).

The goal it is: instead of using a static string as the value for some header, you can use :@ operator to pass the desired value from a file.
Example, let's suppose I cd in a folder with a data.json file with content { "email": "...", "password": "..." } and a file "name.txt" with content as just Thales. You'll can...

treq POST httpbin.org/post \
    [email protected]               # Read a data field’s value from a file
    datas:[email protected]        # Embed a JSON object from a file
    X-Data:@files/another-file.txt             # Read a header from a file
    token==@files/token.txt             # Read a query parameter from a file

Additional Considerations:

This handler should live in input parsers to request data.

It must exist a function to receive input of "file path" and reader it, returning the String of content. This function would be called inside functions of parsers_request_items (that should pass to it the value they extracted from their input).

Add a validation and suggestions for incorrect subcommands

The current behavior of the treq command allows the execution of a request when a subcommand is omitted, potentially leading to confusion. For example, both treq GET url.com and treq url.com execute the same command. However, if the user mistypes a subcommand (e.g., treq GE url.com), it interprets the mistyped text as the URL and additional parameters, submitting a GET request for http://GE and attempting to validate url.com as a Request item.

The proposal is to implement a command validator specifically for default command requests (no subcommand provided). This validator will check for potential mistyped subcommands. If the entered command closely resembles a valid subcommand, provide a helpful suggestion to the user before executing the command.

Expected Behavior:

  • Implement a command validator to identify potential mistyped subcommands for default command requests.
  • Provide suggestions for valid subcommands using string distance calculation (Levenshtein distance).
  • Optionally, continue execution only if the user confirms the suggested correction. (Consider implementing a flag like --no-confirm to skip the confirmation prompt and only display a warning.)

Additional Considerations:

  • Ensure that the string distance calculation is case-insensitive. For example, when a user types treq get or treq GEt, the recommendation to type GET should appear, regardless of the casing.
  • Provide a nice and clear output warning for this help.
  • Conduct thorough testing to verify the accurate of String distance algorithm

Add manual for `man treq` command

The current state of the project lacks comprehensive manual documentation for the man treq command.

Expected Deliverables:

  • Inclusion of usage examples and best practices for effective utilization of treq.
  • Clear explanations of available commands, subcommands, and their respective flags.
  • A well-structured and comprehensive manual documentation accessible through the man treq commands.

Command for remove saved requests

Implement a new command that allows users to remove saved requests from the system.

treq remove my-request

Expected Behavior:

  • Introduce a new command treq remove to remove saved requests.
  • Confirmation Mechanism: Implement a confirmation step to avoid accidental removal of requests.
  • Consider implementing a flag like --no-confirm to skip the confirmation prompt

Command for rename saved requests

Implement a new command that allows users to rename saved requests from the system.

treq rename my-request new-name

You can imagine this command as the same thing of this below, without doing request...

treq run my-request --save-as new-name

Expected Behavior:

  • Introduce a new command treq rename to edit name of saved requests without submit it.
  • Confirmation Mechanism: Implement a confirmation step to avoid accidental rename of requests
  • Consider implementing a flag like --no-confirm to skip the confirmation prompt

Add auto-completion of saved requests to commands like `run` and `inspect`

Enable auto-completion with saved requests. For commands such as run and inspect, when a user types , the system should complete with the names of saved requests.

That way, if user has a lot of saved request, the use of them turn easy.

Possible Solution/Implementation

Since the project utilizes Clap for managing the CLI interface, the proposed solution involves configuring or extending its features.

Auto-completing saved requests can be achieved by reading the NAME of all FILES in {DATA_FOLDER}/collections/. For linux the folder is $HOME/.local/share/treq/collections/.

Check if process is piped and exclude decoration of output.

When user execute TReq in a not TTY enviroment. Redirecting or piping output, like this...

treq url.com > file.txt # redirecting to
treq url.com | grep search-term

The TReq should check this and make the output the basic request output.

This check can be made by this function in Crossterm: https://docs.rs/crossterm/latest/crossterm/tty/trait.IsTty.html

Additional Considerations:

Use the same command options of using --body flag. Make the check of tty in beginning. If it's true, set like user pass the command with this flag. It is the same behavior.

It's also important does not ignore another flags like --header. So, this verification should be the last priority possible in all validation.

Add operator to add non-string JSON fields using `key:=value` operator

The objective is to replicate this feature in HTTPie (https://httpie.io/docs/cli/non-string-json-fields). The proposal suggests introducing a new operator in Request items to add JSON field. Instead of the traditional key=value syntax, users can utilize key:=value. This new separator is designed to automatically infer the type of the field when it's a non-string.

  age:=29                           # Raw JSON — Number
  married:=false                    # Raw JSON — Boolean
  hobbies:='["http", "pies"]'       # Raw JSON — Array
  favorite:='{"tool": "HTTPie"}'    # Raw JSON — Object

Expected Behavior

Implement type inference for the following JSON types

  • Number
  • Boolean (accepting trueor false as values)
  • Objects (leveraging the same validations and parser as the --raw flag)
  • Array (equivalent implementation to Objects)

Additional Considerations:

  • Ensure that the introduced operator is clearly documented and explained in the treq documentation.
  • Conduct thorough testing to verify the accurate inference of JSON types and proper integration with the existing treq command.
  • Consider providing informative error messages in case of invalid JSON structures.

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.