Permalink
Browse files

added -q option for query string when sending data in request body also

  • Loading branch information...
1 parent a4c3239 commit 0a8b2ad157335baecef720f8b98fc1cdcad73712 @micha committed Apr 29, 2011
Showing with 30 additions and 7 deletions.
  1. +14 −4 resty
  2. +4 −1 test/tests
  3. +12 −2 test/tests.out
View
18 resty
@@ -14,7 +14,7 @@ export _resty_path=""
export _resty_nohistory=""
function resty() {
- local confdir host cookies method h2t editor domain _path opt dat res ret out err verbose raw i j d tmpf args2 wantdata vimedit quote
+ local confdir host cookies method h2t editor domain _path opt dat res ret out err verbose raw i j d tmpf args2 wantdata vimedit quote query maybe_query norc
local -a curlopt
local -a curlopt2
@@ -31,16 +31,26 @@ function resty() {
[ -d "$cookies" ] || (mkdir -p "$cookies"; echo "http://localhost*" > "$host")
[ -n "$1" ] && [ "${1#/}" != "$1" ] \
&& _path="$1" && shift
+ [ "$1" = "${1#-}" ] && dat="$1" && shift
j=1
for i in "$@"; do
+ if [ -n "$maybe_query" ]; then
+ if [ "$i" = "${i#-}" ]; then
+ query="?$i"
+ unset norc
+ fi
+ unset maybe_query
+ continue
+ fi
([ "$i" = "--verbose" ] || echo "$i" | grep '^-[a-zA-Z]*v[a-zA-Z]*$' >/dev/null) \
&& verbose="yes" && continue
[ "$i" = "-V" ] && vimedit="yes" && continue
[ "$i" = "-Z" ] || [ "$method" = "HEAD" ] || [ "$method" = "OPTIONS" ] \
&& raw="yes" && continue
[ "$i" = "-W" ] && continue
[ "$i" = "-Q" ] && quote="yes" && continue
+ [ "$i" = "-q" ] && maybe_query="yes" && norc="-q" && continue
curlopt[j]="$i" && j=$((j + 1))
done
@@ -57,7 +67,7 @@ function resty() {
HEAD|OPTIONS|GET|DELETE|POST|PUT|TRACE)
eval "curlopt2=(${_resty_opts[*]})"
dat=$( ( [ "$wantdata" = "yes" ] \
- && ( ( [ -n "$1" ] && [ "${1#-}" = "$1" ] && echo "$1") \
+ && ( ( [ -n "$dat" ] && echo "$dat") \
|| ([ ! -t 0 ] && echo "@-") ) ) || echo)
if [ "$wantdata" = "yes" ] && [ "$vimedit" = "yes" ]; then
tmpf=$(mktemp /tmp/resty.XXXXXX)
@@ -72,9 +82,9 @@ function resty() {
[ "$method" = "HEAD" ] && opt="-I" && raw="yes"
[ "$method" = "OPTIONS" ] && opt="-I" && raw="yes"
eval "args2=( $(cat "$confdir/$domain" 2>/dev/null |sed 's/^ *//' |grep ^$method |cut -b $((${#method}+2))-) )"
- res=$((((curl -sLv $opt "$dat" -X $method \
+ res=$((((curl $norc -sLv $opt "$dat" -X $method \
-b "$cookies/$domain" -c "$cookies/$domain" \
- "${args2[@]}" "${curlopt2[@]}" "${curlopt[@]}" "$_path" \
+ "${args2[@]}" "${curlopt2[@]}" "${curlopt[@]}" "$_path$query" \
|sed 's/^/OUT /' && echo) 3>&2 2>&1 1>&3) \
|sed 's/^/ERR /' && echo) 2>&1)
out=$(echo "$res" |sed '/^OUT /s/^....//p; d')
View
@@ -60,6 +60,9 @@ resty 127.0.0.1:9090 2> /dev/null
# TEST 14: make sure the default options are correctly unset
GET /simple.json
-# TEST 15: try PUT with both data in body and query params in url
+# TEST 15: try PUT with both data in body and query params in url, no urlencode
echo '{"foo":"bar"}' |PUT '/simple.json?param=value&other=blah' -Q
+# TEST 16: try PUT with both data in body and query params in url via -q
+echo '{"foo":"bar"}' |PUT '/simple.json' -q 'param=value&other=blah'
+
View
@@ -103,7 +103,15 @@ GET /simple.json HTTP/1.1
User-Agent: curl/7.21.2 (x86_64-apple-darwin10.4.0) libcurl/7.21.2 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.20
Host: 127.0.0.1:9090
Accept: */*
-# [REQ] TEST 15: try PUT with both data in body and query params in url
+# [REQ] TEST 15: try PUT with both data in body and query params in url, no urlencode
+PUT /simple.json?param=value&other=blah HTTP/1.1
+User-Agent: curl/7.21.2 (x86_64-apple-darwin10.4.0) libcurl/7.21.2 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.20
+Host: 127.0.0.1:9090
+Accept: */*
+Content-Length: 14
+Content-Type: application/x-www-form-urlencoded
+{"foo":"bar"}
+# [REQ] TEST 16: try PUT with both data in body and query params in url via -q
PUT /simple.json?param=value&other=blah HTTP/1.1
User-Agent: curl/7.21.2 (x86_64-apple-darwin10.4.0) libcurl/7.21.2 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.20
Host: 127.0.0.1:9090
@@ -517,5 +525,7 @@ http://github.com/micha/resty
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}
# [OUT] TEST 14: make sure the default options are correctly unset
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}
-# [OUT] TEST 15: try PUT with both data in body and query params in url
+# [OUT] TEST 15: try PUT with both data in body and query params in url, no urlencode
+{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}
+# [OUT] TEST 16: try PUT with both data in body and query params in url via -q
{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}

0 comments on commit 0a8b2ad

Please sign in to comment.