HTTPサーバーのテストで、手軽にサンプル送信するときの備忘録。なおサーバー側のサンプルコードはNode.js+Express。
get送信
クライアント側
同名カッコにすると、受け側で配列になる。
curl -G -d "a=123&b=abc&c[]=9&c[]=8&c[]=7" http://localhost:8080/test01
サーバー側
app.get( '/test01', function( req, res ){
console.log("---- [get] /test01 ----");
console.log(req.query);
res.json( {status:0} );
});
結果
---- [get] /test01 ----
{ a: '123', b: 'abc', c: [ '9', '8', '7' ] }
GET /test01?a=123&b=abc&c[]=9&c[]=8&c[]=7 200 3.368 ms - 12
値がすべて文字列になっているので注意。
post送信
クライアント側
送信データはJSONを文字列化したものを送信する。
curl -X POST -H "Content-Type: application/json" -d '{"a":123,"b":"abc","c":[9,8,7]}' http://localhost:8080/test02
サーバー側
app.post( '/test02', function( req, res ){
console.log("---- [post] /test02 ----");
console.log(req.body);
res.json( {status:0} );
});
結果
---- [post] /test02 ----
{ a: 123, b: 'abc', c: [ 9, 8, 7 ] }
POST /test02 200 3.156 ms - 12
BASIC認証付き
--user user:password
形式のオプションを追加する
> curl -X POST -H "Content-Type: application/json" -d '{"a":123,"b":"abc","c":[9,8,7]}' --user hoge:huga http://localhost:8080/test02
var basic_auth = require( 'basic-auth-connect' )
app.all('*', basic_auth(function(user, password) {
var ret_auth = false;
console.log("----------------------------------->");
console.log("[user]"+user);
console.log("[password]"+password);
// true:認証成功
// false:認証エラー
ret_auth = true;
return ret_auth;
}));