1
1

More than 3 years have passed since last update.

Node.jsのrequest-promiseモジュールでresponseの内容が表示できずはまった件

Posted at

やりたかったこと

Node-REDでリビングのGoogle homeにメッセージを喋らせるREST APIを作り、Node.jsでそのREST APIを呼び出すプログラムを作りたかった。REST APIの呼び出しにはrequest-promiseモジュールを利用。

はまったところ

REST APIを以下のコードで呼び出したが、responseの内容を表示しようとしてもコンソールにundefinedと表示されてしまう。

 var options = {  
        url: 'http://XXX.XXX.XXX.XXX:1880/sendtogh',  
        method: 'POST',  
        form: {"message": message},  //前段の処理でmessageにgoogle homeに喋らせたい内容をセット
      }  
    request(options)  
    .then(function(response){  
        console.log(response.statusCode);  //コンソールに「undefined」と表示される  
    })  
    .catch(function(err){  
    (省略)

解決方法

request-promiseのドキュメントを見ると、optionsにresolveWithFullResponse: trueを指定しないと、bodyの内容しか返却されないとのこと。
https://github.com/request/request-promise#get-the-full-response-instead-of-just-the-body

1
1
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
1
1