LoginSignup
3
0

More than 5 years have passed since last update.

Espruino で HTTP GET リクエストは例があるので簡単にできる。POST でデータ送信する例がなかったので、調べたメモ。

var data = JSON.stringify({"metrics1":10.0});
var request = require("http").request(
    {host:"harvest.soracom.io", method:"POST", path:"/", headers:{
        "content-type":"application/json",
        "content-length": data.length,
        "connection":"close"
    }},
    response=>{
        var content = "";
        response.on("data", data=>{
            content += data;
        });
        response.on("close", ()=>{
            console.log(content);
        });
        response.on("error", err=>{
            console.log("error", err);
        });
    });
request.write(data);
request.end();

Content-length は、サーバ側の実装の都合上、たいてい必要になる。Connection: close は、開発時サーバが single thread の時のための安全設定。

3
0
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
3
0