Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the post request support removed in ganda? #10

Open
TechieGenie opened this issue Oct 8, 2024 · 5 comments
Open

Is the post request support removed in ganda? #10

TechieGenie opened this issue Oct 8, 2024 · 5 comments

Comments

@TechieGenie
Copy link

I'm trying to make a couple of post requests using -d flag like -d {<body goes here>}

I see that ganda errors out saying Incorrect Usage: flag provided but not defined: -d

cat << EOF > /tmp/list_of_urls_and_values
http://httpbin.org/anything/1 bar 111
http://httpbin.org/anything/2 baz 222
http://httpbin.org/anything/3 qux 333
EOF
cat /tmp/list_of_urls_and_values | ganda -X POST -d '{"first": "%s", "second": "%s"}' | grep -E 'data|url'

Example from this page - https://www.naleid.com/2018/04/04/using-http-apis-on-the-command-line-3-ganda.html

@TechieGenie
Copy link
Author

ganda -v

ganda version 1.0.3 7d01150a459b2ebb112f6b317f4a782eb5adf0fa 2024-07-30T23:04:12Z

@tednaleid
Copy link
Owner

tednaleid commented Oct 8, 2024

That post was written prior to ganda 1.0. I did remove the body template parameter (-d) from ganda as it was limited in how expressive it could be and there are other CLI tools that are better ad

The tour of ganda has some examples, but here's the equivalent way to do the same thing by streaming JSON lines to ganda.

Use jq to create JSON (schema for input JSON) that we can stream to ganda:

cat << EOF | jq -Rc 'split(" ") | {url: .[0], body: {first: .[1], second: .[2]}}'                             
http://httpbin.org/anything/1 bar 111
http://httpbin.org/anything/2 baz 222
http://httpbin.org/anything/3 qux 333
EOF
{"url":"http://httpbin.org/anything/1","body":{"first":"bar","second":"111"}}
{"url":"http://httpbin.org/anything/2","body":{"first":"baz","second":"222"}}
{"url":"http://httpbin.org/anything/3","body":{"first":"qux","second":"333"}}

then with ganda and the grep:

cat << EOF | jq -Rc 'split(" ") | {url: .[0], body: {first: .[1], second: .[2]}}' | ganda | grep -E 'data|url'
http://httpbin.org/anything/1 bar 111
http://httpbin.org/anything/2 baz 222
http://httpbin.org/anything/3 qux 333
EOF
Response: 200 http://httpbin.org/anything/1
  "data": "{\"first\":\"bar\",\"second\":\"111\"}", 
  "url": "http://httpbin.org/anything/1"
  "data": "{\"first\":\"baz\",\"second\":\"222\"}", 
Response: 200 http://httpbin.org/anything/2
  "url": "http://httpbin.org/anything/2"
Response: 200 http://httpbin.org/anything/3
  "data": "{\"first\":\"qux\",\"second\":\"333\"}", 
  "url": "http://httpbin.org/anything/3"

@TechieGenie
Copy link
Author

Is it possible to bring back that option? It was very intuitive. I used to get a curl from postman or any other place and used to replace curl with ganda to craft requests by substituting a string in the body.

@tednaleid
Copy link
Owner

I haven't used ganda + postman like you're suggesting, so I might not be fully following how the old system was compatible with it. Do curl and postman use that same %s token replacement with the url in the same first position?

I'd lean a lot more into making some pipe utility that can format JSON like ganda now wants but do it in a flexible way. Turning the jq statment I have above into a separate CLI that supports a -d '{body}' command would be pretty easy.

With ganda, I want it to do one thing well: making lots of http requests and handling responses. It was a pretty bad template language that I had in the 0.X ganda days and I'd rather delegate that to tools more custom suited to nice formatting.

If you can show me some commands of how you're taking a curl and turning it into a ganda statement, I'd be interested in seeing that. I might be missing something in the use case.

@tednaleid
Copy link
Owner

tednaleid commented Oct 8, 2024

I threw together a quick golang CLI tool that could be used for generic values like this called gotemplate, it can use all of the various printf style values (so, significantly more control than the 0.x version of ganda had):

cat << EOF | ./gotemplate -d '{"string": "%s", "int": %5d, "left_int": %-5d, "zero_pad": %05d, "float": %8.2f, "left_float": %-8.2f, "generic": %v}'
hello 42 99 7 3.14159 2.718 world
goodnight 1234 5 123 0.1 100.5 3.1415
EOF
{"string": "hello", "int":    42, "left_int": 99   , "zero_pad": 00007, "float":     3.14, "left_float": 2.72    , "generic": world}
{"string": "goodnight", "int":  1234, "left_int": 5    , "zero_pad": 00123, "float":     0.10, "left_float": 100.50  , "generic": 3.1415}

This could be used to generate values that could be passed to ganda, but would also be generically useful for templating stdin and "form filling" values:

cat << EOF | ./gotemplate -d '{"url": "%s", "body": {"first": "%s", "second": %d}}'
http://httpbin.org/anything/1 bar 111
http://httpbin.org/anything/2 baz 222
http://httpbin.org/anything/3 qux 333
EOF
{"url": "http://httpbin.org/anything/1", "body": {"first": "bar", "second": 111}}
{"url": "http://httpbin.org/anything/2", "body": {"first": "baz", "second": 222}}
{"url": "http://httpbin.org/anything/3", "body": {"first": "qux", "second": 333}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants