17
11

More than 5 years have passed since last update.

node.jsのrequestモジュールでContent-Type: application/jsonのHTTP POSTリクエストを行う

Posted at

背景

FCM (Firebase Cloud Messaging) を利用してプッシュ通知を行う Android/iOS アプリを作成したので、node.js の request モジュールを利用して FCM Server への HTTP POST リクエストを行い動作確認しようとしたところ、期待したメッセージが届かない。。。

FCM Server リクエストフォーマット

https://firebase.google.com/docs/cloud-messaging/server より

HTTP-header
Content-Type: application/json
json-example
{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

失敗例

bad.js
var request = require('request');

require('date-utils');
var date = new Date();
var dateString = date.toFormat("YYYY-MM-DD HH24:MI:SS");

var url = 'https://fcm.googleapis.com/fcm/send';
var serverkey = 'AIzaSyZ-1u...0GBYzPu7Udno5aA';
var token = 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...';

// data payload
var data = {
  message: 'Hello!',
  date: dateString
};

// HTTP header
var headers = {
  'Content-Type': 'application/json',
  'Authorization': 'key=' + serverkey
};

// request options
var options = {
  url: url,
  method: 'POST',
  headers: headers,
  form: {
    'to': token,
    'data': data
  }
};

request(options, function (error, response, body) {
  if (body) {
    console.log(body);
  }
  if (error) {
    console.log(error);
  }
});

実行結果

Anddroid アプリで受け取ったメッセージのdataを確認したところ、下記のようにkeyに]がついている。

{message]=Hello!, date]=2016-08-12 22:45:45}

送信したHTTPリクエストの確認

localhost 宛にリクエストを送信しパケットキャプチャで確認したところ、以下のようなリクエストを送信していた。

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: key=SERVER_KEY
host: localhost:3000
content-length: 83
Connection: keep-alive

to=DEVICE_TOKEN&data%5Bmessage%5D=Hello%21&data%5Bdate%5D=2016-08-12%2022%3A39%3A47

Content-Type が application/x-www-form-urlencoded になっており、body も JSON になっていない。。。

成功例

request options に form ではなく、json を利用する。

good.js
var request = require('request');

require('date-utils');
var date = new Date();
var dateString = date.toFormat("YYYY-MM-DD HH24:MI:SS");

var url = 'https://fcm.googleapis.com/fcm/send';
var serverkey = 'AIzaSyZ-1u...0GBYzPu7Udno5aA';
var token = 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...';

// data payload
var data = {
  message: 'Hello!',
  date: dateString
};

// HTTP header
var headers = {
  'Content-Type': 'application/json',
  'Authorization': 'key=' + serverkey
};

// request options
var options = {
  url: url,
  method: 'POST',
  headers: headers,
  json: {
    'to': token,
    'data': data
  }
};

request(options, function (error, response, body) {
  if (body) {
    console.log(body);
  }
  if (error) {
    console.log(error);
  }
});

実行結果

{date=2016-08-12 22:46:11, message=Hello!}

HTTP リクエスト

POST / HTTP/1.1
Authorization: key=SERVER_KEY
host: localhost:3000
accept: application/json
content-type: application/json
content-length: 78
Connection: keep-alive

{"to":"DEVICE_TOKEN","data":{"message":"Hello!","date":"2016-08-12 22:41:36"}}

おわりに

ちゃんとドキュメントを読みましょう。
https://github.com/request/request

17
11
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
17
11