You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.4 KiB

package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func main() {
// Name of the executable
exeName := "poster"
// Help Message (-h, --help)
helpMsg := `Usage: %[1]s [FLAGS] URL
Execute HTTP-Requests and print responses to stdout
-m <method> Method to use for the request (default "get")
POST '{"mydata": "string", "nene": 31}' https://rest.api/lele/lala
PUT '{"mydata": "string", "nene": 31}' https://rest.api/lele/lala/31
PATCH '{"mydata": "string", "nene": 31}' https://rest.api/lele/lala/31
Note: Use "" around everything but numbers, also wrap whole json in ''
-h, --help Print this message.
Examples:
%[1]s https://rest.api/posts/1337
%[1]s -m PUT '{"fooo": "bäü߀r", "userId": 42}' https://rest.api/posts/1
%[1]s -m DELETE https://rest.api/posts/1
`
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), helpMsg, exeName)
}
// Name, default val, help
m := flag.String("m", "get", "Method for the request.")
flag.Parse()
// Number of all flags (executable name included -> 0 indexed)
l := len(os.Args) - flag.NArg()
// Only get the arguments after all flags
args := os.Args[l:]
al := len(args)
// Requested url/web resource
resource := args[al - 1]
// http client used for PUT, PATCH, DELETE
client := &http.Client{}
method := strings.ToLower(*m)
switch method {
// -------------------------------------------------------------------------
// GET
// -------------------------------------------------------------------------
case "get":
fmt.Println("GET -> " + resource + " reported:")
res, err := http.Get(resource)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// Read everything from body
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", body)
// -------------------------------------------------------------------------
// HEAD
// -------------------------------------------------------------------------
case "head":
fmt.Println("HEAD -> " + resource + " reported:")
res, err := http.Head(resource)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", res.Header)
// -------------------------------------------------------------------------
// POST
// -------------------------------------------------------------------------
case "post":
fmt.Println("POST -> " + resource + " reported:")
if al != 2 {
msg := `Error: Wrong number of arguments
Usage: %s -m POST '{"mydata": "string", "nene": 31}' https://a-website.org/lele/lala
Note: Use "" around everything but numbers, also wrap json in ''
`
fmt.Printf(msg, exeName)
os.Exit(1)
}
data := args[0]
res, err := http.Post(resource, "application/json; charset=UTF-8", bytes.NewBufferString(data))
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", body)
// -------------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------------
case "put":
fmt.Println("PUT -> " + resource + " reported:")
if al != 2 {
msg := `Error: Wrong number of arguments
Usage: %s -m PUT '{"mydata": "string", "nene": 31}' https://a-website.org/lele/lala/123
Note: Use "" around everything but numbers, also wrap json in ''
`
fmt.Printf(msg, exeName)
os.Exit(1)
}
data := args[0]
req, err := http.NewRequest(http.MethodPut, resource ,bytes.NewBufferString(data))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", body)
// -------------------------------------------------------------------------
// PATCH
// -------------------------------------------------------------------------
case "patch":
fmt.Println("PATCH -> " + resource + " reported:")
if al != 2 {
msg := `Error: Wrong number of arguments
Usage: %s -m PUT '{"mydata": "string", "nene": 31}' https://a-website.org/lele/lala/123
Note: Use "" around everything but numbers, also wrap json in ''
`
fmt.Printf(msg, exeName)
os.Exit(1)
}
data := args[0]
req, err := http.NewRequest(http.MethodPatch, resource ,bytes.NewBufferString(data))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", body)
// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
case "delete":
fmt.Println("DELETE -> " + resource + " reported:")
req, err := http.NewRequest(http.MethodDelete, resource, nil)
if err != nil {
log.Fatal(err)
}
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n%s\n\n", body)
// -------------------------------------------------------------------------
// DEFAULT
// -------------------------------------------------------------------------
default:
fmt.Println("Method unknown, use: GET, HEAD, POST, PUT, PATCH, DELETE")
}
}