hpost: support custom headers (thanks julienxx)

hget supports adding custom headers with -r;
it makes sense for hpost to do the same, both
because custom headers are more likely necessary
with POSTs, and for consistency.
This commit is contained in:
Ori Bernstein 2020-12-17 20:26:38 -08:00
parent 658c1b9f68
commit c2aa8c997a
2 changed files with 13 additions and 2 deletions

View file

@ -1,12 +1,15 @@
#!/bin/rc #!/bin/rc
rfork e rfork e
url=() url=()
headers=()
action=()
method=()
at=() # text fields at=() # text fields
af=() # file fields af=() # file fields
l=() l=()
fn usage { fn usage {
echo 'usage: hpost [ -l ] [ -[gpm] action ] [ -u ] url [ field:value | field@file ... ]' >[1=2] echo 'usage: hpost [ -l ] [ -[gpm] action ] [ -r ] header [ -u ] url [ field:value | field@file ... ]' >[1=2]
exit usage exit usage
} }
@ -14,6 +17,7 @@ while(~ $1 -*){
switch($1){ switch($1){
case -l; l=($l $1) case -l; l=($l $1)
case -u; shift; url=$1 case -u; shift; url=$1
case -r; shift; headers=($headers -r $1)
case -g; shift; action=$1; method=mget case -g; shift; action=$1; method=mget
case -p; shift; action=$1; method=mpost case -p; shift; action=$1; method=mpost
case -m; shift; action=$1; method=multi case -m; shift; action=$1; method=multi
@ -94,8 +98,9 @@ fn multi {
menc $"f | $hget -r 'Content-Type: multipart/form-data; boundary='$"f -b $url -P $action menc $"f | $hget -r 'Content-Type: multipart/form-data; boundary='$"f -b $url -P $action
} }
if(! ~ $action ''){ if(! ~ $action ''){
hget=(hget $l) hget=(hget $headers $l)
$method $method
exit exit
} }

View file

@ -38,6 +38,9 @@ hget, hpost, webpaste, urlencode \- retrieve, post to a web page corresponding t
.B -m .B -m
.I action .I action
] [ ] [
.B -r
.I header
] [
.B -u .B -u
] ]
.I url .I url
@ -119,6 +122,9 @@ Option
.B -r .B -r
sends an arbitrary HTTP sends an arbitrary HTTP
.IR header . .IR header .
The
.B -r
flag can be repeated to send multiple headers.
.PP .PP
Option Option
.B -m .B -m