LoginSignup
65

More than 5 years have passed since last update.

posted at

curlでJSONをPOSTする

curlコマンドでPOST

-dでPOSTのBODYを指定。
なんとなくUserAgentにiPhoneやgzip要求もつけてみる。

curl -H 'Content-Type:application/json' -H 'User-Agent:iPhone' -H 'Accept-Encoding:gzip,deflate' -d "{"key":"val","key2":",val2"}" http://~~~~~~~~~~

[おまけ]同じPOSTを何回もするShellスクリプト

URL、回数、POSTするJSONを引数に持ち、1秒間隔を空けながら複数回同様のPOSTをする。
色々変えて使うといいかも。

#!/bin/sh

# Usage: xxxxxx.sh url times json
#
# Arguments:
#   url         
#   times       
#   json        it MUST be surrounded single-quartations
#
# Example
#   sh xxxxxx.sh http://localhost:8080/webapi/user/XXXXX 10 '{"token":"xxxxx","version":"1.3"}'
#

URL=$1
TIMES=$2
JSON=$3

if [ -z "$URL" ]; then
   echo "no url"
   exit;
fi
if [ -z "$TIMES" ]; then
   echo "no times"
   exit;
fi
if [ -z "$JSON" ]; then
   echo "no json"
   exit;
fi

n=1
until [ $n -gt $TIMES ]
do
  curl -H 'Content-Type:application/json' -H 'User-Agent:iPhone' -d "$JSON" $URL
    n=$(( n+1 ))
  echo ""
  sleep 1s
done

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
65