LoginSignup
6
6

More than 5 years have passed since last update.

CasperJSでJSONデータをPOSTする

Last updated at Posted at 2014-12-31

requestのbodyにJSONを入れてPOSTする必要のあるAPIが最近よくありますが、そのAPIのE2Eテストをcasperjsでする時に意外とやり方が見つからなかったので短いですがメモ。

coffee scriptなのでJSな人は適当に読み替えてください。

casper.thenOpen "http://localhost:8080/xxxx",
  method: "post"
  data: JSON.stringify({
    title:"title1"
    content: "foobar"
    })
  headers:
    "Content-Type": "application/json; charset=utf-8"
, ->
  @echo "POST request has been sent."
  test.assertHttpStatus 201

サーバー側の仕様にもよると思いますが、Content-Typeヘッダーを付けるのと、単にJSON.stringifyしてあげるという話でした。いまいちググってもやり方が見つからず、試行錯誤した結果こうでした。

6
6
0

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
  3. You can use dark theme
What you can do with signing up
6
6