post a json object with an array using curl

来源:互联网 发布:网络信息安全责任状 编辑:程序博客网 时间:2024/05/07 03:45

在测试API时,为了排除session等的干扰,使用curl是一个不错的选择,那么如何使用curl向API传递JSON对象或数组呢?

以下的代码便可实现:


curl -v -X POST \  -H "content-type:application/json" \  "http://URL/api/1/testApi" \  -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}'


服务器端的接受:


app.post('/api/1/testApi',function(req,res){console.log(req.body.question);console.log(req.body.tags);var tags = req.body.tags;console.log(tags[0]);var answers = req.body.answers;console.log(answers[0]);var answer0 = answers[0];console.log(answer0.id);});

very good!

0 0
原创粉丝点击