2
0
Fork 0
mirror of https://github.com/andrewpillar/req.git synced 2025-10-08 11:24:23 +02:00
A simple and opinionated HTTP scripting language
Find a file
Andrew Pillar b9f4e7ada6 Add uuid command for creating UUIDs
This would be used in instances where you may want to set a request ID
via the X-Request-Id header.
2022-11-16 19:00:09 +00:00
docs add uuid command to create UUIDs (v4) 2022-11-15 12:08:32 +01:00
eval apply suggested changes from PR 2022-11-16 08:46:07 +01:00
examples Update string interpolation to use $() instead of {} 2022-01-22 21:01:00 +00:00
syntax gofmt -s -w 2022-06-12 13:43:41 +01:00
value Unexport tuple fields 2022-06-14 14:27:02 +01:00
version Comments 2022-01-16 15:34:58 +00:00
.gitignore Begin work on program entrypoint 2021-12-27 18:42:30 +00:00
go.mod add uuid command to create UUIDs (v4) 2022-11-15 12:08:32 +01:00
go.sum add uuid command to create UUIDs (v4) 2022-11-15 12:08:32 +01:00
LICENSE Add LICENSE 2022-01-02 17:56:23 +00:00
main.go Set terminal to raw mode for REPL 2022-01-25 19:05:57 +00:00
make.sh Update git build info 2022-01-27 19:49:29 +00:00
readme.md Typo fix in readme 2022-03-07 22:06:38 +00:00

req

req is an opinionated HTTP scripting language. It is designed for easily making HTTP requests, and working with their responses. Below is an example that calls out to the GitHub API and displays the user making the call,

$ cat gh.req
Stderr = open "/dev/stderr";

Endpoint = "https://api.github.com";
Token = env "GH_TOKEN";

if $Token == "" {
    writeln $Stderr "GH_TOKEN not set";
    exit 1;
}

Headers = (
    Authorization: "Bearer $(Token)",
);

Resp = GET "$(Endpoint)/user" $Headers -> send;

match $Resp.StatusCode {
    200 -> {
        User = decode json $Resp.Body;

        writeln _ "Hello $(User["login"])";
    }
    _   -> {
        writeln $Stderr "Unexpected response: $(Resp.Status)";
        exit 1;
    }
}
$ GH_TOKEN=1a2b3c4d5ef req gh.req

This language hopes to fill in a gap when it comes to writing scripts for working with an HTTP service. Typically, you have a choice between a shell script that utilizes cURL, or a programmaing language and any HTTP library that may come with it.

The cURL approach can work, for simple one off requests, but when you want to do something more with the response you're left with having to munge that data with jq, grep, sed, or awk (or all of the above). Using a programming language gives you more control, but can be more cumbersome as you have far many more knobs to turn.

req provides a middleground between the two. A limited syntax, with builtin commands for working with any data you want to send/received. For more details on how to start working with req, then refer to the documentation, or you can dive right in by looking over the examples.