shrijan00003 / restler

A Simple file and git compatible rest client
6 stars 1 forks source link

Restler

Restler is a file based REST API client. If you are familiar with tools like Postman, Insomnia, or curl, then you will feel right at home with Restler. This is file based, opinionated, command line tool. Please follow Installation and Usage guide to get started.

Installation

MacOS x86_64

curl -s https://raw.githubusercontent.com/shrijan00003/restler/main/install/darwin-amd64.sh | bash

MacOS arm64

curl -s https://raw.githubusercontent.com/shrijan00003/restler/main/install/darwin-arm64.sh | bash

Linux x86_64

curl -s https://raw.githubusercontent.com/shrijan00003/restler/main/install/linux-amd64.sh | bash

Getting Started

Project Initialization and Structure

1. Initialization using restler init:

2. Manual Project Structure Creation:

This approach allows for a more customized project structure and configuration for your specific needs.

User Guide

  1. Create a folder for the API collection, default is restler.
  2. Create a folder requests/<request-name> like requests/posts inside the API collection folder and create a request file inside it.
  3. Create a request file with .<http-method>.yaml extension. For example posts.post.yaml, or posts.get.yaml
  4. (Optional) Create env folder inside the API collection folder and create a .yaml file inside it. For example env/default.yaml for using environment variables in request.
  5. (Optional) Create config.yaml file inside the API collection folder for configurations like environment.
  6. (Optional) Change folder for API collection in runtime by setting RESTLER_PATH environment variable. For example export RESTLER_PATH=app-prefix-collection
  7. Run restler <http-method> <request-name> to run the request. For example restler psot posts to run post request and restler get posts to run get request.
  8. Check the output files in requests/<request-name> folder. For example requests/posts/.<request-name>.post.res.md for post response and requests/posts/.get.res.txt for get request response.
  9. for other supports please check the TODO file.

Commands Available

Flag support

Now all our REST method commands supports following flags:

--env or -e

env flag is compatible for selecting environment from the command line. If we don't pass this option in command Env from config.yaml is default.

Usage

restler <http-command> --env <env-value> <request name>

Fog eg,

restler p -e dev posts

--request or -r

request flag is useful for seleting individual request from the request collection if you have multiple requests of same http method. Consider following structure:

reslter
    requests
        posts
            - posts.post.yaml
            - posts-v2.post.yaml

For running posts-v2, we can use -r like following:

restler p -r posts-v2 posts

Setup Environment Variables from Response

For setting environment variables from response, we can use After section in request file. For example:

Name: Create Post
URL: "{{API_URL}}"
Method: POST

Headers: ...

Body: ...

After:
  Env:
    ADDRESS_STREET: Body[address][street]
    HOBBIES: Body[hobbies][0]
    RESPONSE_DATE: Header[Date]
    EMPLOYMENT_START_DATE: Body[employment][details][start_date]

Here, Body and Header are special keys for accessing response body and response headers respectively. For accessing, we can use Body[key] or Header[key] syntax. If you have array like structure, you can access it using Body[key][index] or Header[key][index]. If value is not found, it will write empty string in your env file.

Proxy Usage

Restler respects the HTTPS_PROXY and HTTP_PROXY environment variables. You can specify a proxy URL for individual requests using the R-Proxy-Url header. To disable the proxy for specific requests, use the R-Proxy-Enable: N header.

for eg:

Name: Get Posts
URL: "{{API_URL}}"
Method: GET

Headers:
  Accept: text/html, application/json
  Accept-Encoding: utf-8
  R-Proxy-Url: https://something.com:8080
  R-Proxy-Enable: N # N or Y, Y is default, if HTTPS_PROXY, HTTP_PROXY or R-Proxy-Url is set
  User-Agent: rs-client-0.0.1
  Content-Type: application/json

Body:

Inspiration

Build

go build -o restler bin/main.go