MissingProperty error in Microsoft Bot Framework Request - javascript

I am working on an app that uses the Microsoft Bot Framework. My app is written in Node. At this time, I am trying to POST an activity using the following code:
var https = require('https');
var token = '[receivedToken]';
var conversationId = '[conversationId]';
var options = {
host: 'directline.botframework.com',
port: 443,
headers: {
'Authorization': 'Bearer ' + token'
},
path: '/v3/directline/conversations/' + conversationId + '/activities',
method: 'POST'
};
var request = https.request(options, (res) => {
console.log(res.statusCode);
var body = [];
res.on('data', (d) => {
body.push(d);
});
res.on('end', () => {
var result = JSON.parse(Buffer.concat(body).toString());
console.log(result);
});
});
var info = {
type: 'message',
text: 'test',
from: { id: 'user_' + conversationId }
};
request.write(querystring.stringify(info));
request.end();
request.on('error', (err) => {
console.log(err);
});
When this code is ran, I receive an error. It's an error of status code 400 which has the following:
{
error: {
code: 'MissingProperty',
message: 'Invalid or missing activities in HTTP body'
}
}
I don't understand what property is missing though. Everything looks correct.

You missed Content-Type and Content-Length in your request headers.
Please consider the following code snippet:
var https = require('https');
var token = '[receivedToken]';
var conversationId = '[conversationId]';
var info = JSON.stringify({
type: 'message',
text: 'test',
from: { id: 'user_' + conversationId }
})
var options = {
host: 'directline.botframework.com',
port: 443,
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(info)
},
path: '/v3/directline/conversations/' + conversationId + '/activities',
method: 'POST'
};
var request = https.request(options, (res) => {
console.log(res.statusCode);
var body = [];
res.on('data', (d) => {
body.push(d);
});
res.on('end', () => {
var result = JSON.parse(Buffer.concat(body).toString());
console.log(result);
});
});
request.write(info);
request.end();
request.on('error', (err) => {
console.log(err);
});

Related

Why am I getting the error "'request' is not defined"?

I'm trying to create an app with the Spotify API, but can't seem to get it to work. The error I'm getting is that 'request' is undefined and I've replaced it with JQuery too and that doesn't work either. Can anyone tell me why I might be getting that error and how to fix it? Should I be running it inside node.js in cmd?
var client_id = '?';
var client_secret = '?';
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
form: {
grant_type: 'client_credentials'
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
var token = body.access_token;
}
else {
console.log(JSON.stringify(error))
}
});
The spotify documentation is out of date, as request is deprecated, and should no longer be used.
Instead, you can make a request with built-in Node.js libraries, as mentioned in the documentation.
It should be run with node.js, i.e. node <filename>
const https = require('https')
const client_id = 'CLIENT_ID'
const client_secret = 'CLIENT_SECRET'
const reqBody = JSON.stringify({
grant_type: 'client_credentials'
})
const authOptions = {
hostname: 'accounts.spotify.com',
port: 443,
path: '/api/token',
method: 'POST',
headers: {
'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64')),
'Content-Type': 'application/json',
'Content-Length': reqBody.length
}
}
const req = https.request(authOptions, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.write(reqBody);
req.end();

Node.js http post request with basic authentication

I can make this work with axios but as I want to do this with default http module for some reasons
Here is the code
var express = require("express");
const http = require('https');
var app = express();
app.listen(3000, function () {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostName: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': data.length
}
};
const req = http.request(options, (res) => {
console.log('response is ' + res);
});
req.on('error', (error) => {
console.log('error is ' + error);
});
});
But it is throwing exception
connect ECONNREFUSED 127.0.0.1:443
It seems like you're providing the wrong options object (perhaps copied over from axios). The Node.js HTTP module takes host or hostname in options, while you're providing hostName.
Reference: https://nodejs.org/api/http.html#http_http_request_options_callback
I'm not entirely sure what you're trying to do. Any reason why you need your application to listen? I'm assuming the application you're posting to is hosted somewhere else, as you're attempting to listen to port 3000 while also making the request to an application on port 3000. If each application is on a different host, this should be fine. Nonetheless, you've at least got 3 issues here.
1) Your options object is incorrect. You're using hostName when it should be hostname. This is why you get the ECONNREFUSED 127.0.0.1:443 error; the options object for the https.request() method defaults hostname to localhost and port to 443.
2) Also, you never write your data object contents to the request stream.
3) Finally, you should listen to the data event to get the response back and write it to the console. I've updated your code as shown below:
var express = require("express");
const http = require('https');
var app = express();
app.listen(3000, function () {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostname: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': Buffer.byteLength(data)
}
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (error) => {
console.log('error is ' + error);
});
req.write(data);
req.end();
});
You cannot move your express application to AWS Lambda as is. There are tools such as claudia which can help you move the app to lambda and api gateway.
In your case, you can modify your code AWS Lambda as below
const http = require('https');
exports.myHandler = function (event, context, callback) {
var username = 'username';
var password = 'password';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const data = JSON.stringify({
campaign_id: 'all',
start_date: '01/01/2010',
end_date: '05/31/2030',
return_type: 'caller_view',
criteria: {
phone: 98855964562
}
});
var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";
const options = {
hostName: hostName,
path: path,
port: 3000,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'Content-Length': data.length
}
};
const req = http.request(options, (res) => {
console.log('response is ' + res);
callback(null, res);
});
req.on('error', (error) => {
console.log('error is ' + error);
callback(error);
});
}
You have to invoke your lambda via API Gateway or via other AWS resources such as Alexa Skill Kit etc.
EDIT
You may try passing auth options as specified # https://github.com/request/request/blob/master/README.md#http-authentication

Reading incoming HTTPS headers from Spring Boot 2 response with node.js

Below in this example, in the variable 'obj' i get body of response. How to get header values of response using this https node.js library?
var options = {
hostname: hostname,
port: port,
path: pathMethod,
method: method,
headers: {
'Content-Type': APPLICATION_JSON,
'Authorization': BEARER + localStorage.jwtToken
},
rejectUnauthorized: false,
agent: false,
requestCert: false
};
return new Promise(function(resolve, reject) {
var req = https.request(options, function(res) {
res.setEncoding(ENCODING_UTF8);
res.on('data', function(result) {
try {
const obj = JSON.parse(result);
resolve({ 'httpStatus': PAGE_STATUS_200, 'result': obj });
}
catch(error) {
console.error(error);
resolve(resolve({ 'httpStatus': PAGE_STATUS_500 }));
}
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', function(err) {
console.log(`problem with request: ${err.message}`);
reject(err);
});
if (postData) {
req.write(postData);
}
req.end();
});
In my browser i get all necessary headers. What could be the problem that i can not get headers with https node.js lib?
You can get the headers in https module.
This is how you get the headers for the response.
res.headers
I have updated your code in example below:
var req = https.request(options, function(res) {
res.setEncoding(ENCODING_UTF8);
res.on('data', function(result) {
console.log("Headers: ", res.headers);
// Your code here.
});
res.on('end', () => {
// Do something here.
});
});
Hope this helps.
The response headers should be available in the res.headers object, e.g.
// Log headers
console.log('Headers: ', res.headers);
See: https://nodejs.org/api/https.html
e.g.
const https = require ('https');
// This will return the IP address of the client
var request = https.request({ hostname: "httpbin.org", path: "/ip" }, (res) => {
console.log('Headers: ', res.headers);
res.on('data', (d) => {
console.log('/ip response: ', d.toString());
});
});
// Also try using Request library
var request = require('request');
var options = {
url: "https://httpbin.org/ip",
method: "get"
};
console.log('Requesting IP..');
request(options, function (error, response, body) {
if (error) {
console.error('error:', error);
} else {
console.log('Response: Headers:', response && response.headers);
}
});

Function with callback doesn't retrieve result out of my HTTP POST call

I want to make a REST call and get the result out in a variable (access_token). My variable AFAIK is global. Why is my variable access_token undefined at the end, even though i get a result in the console ? I understand that this call is async, but I put a 'false' in the call.
var https = require('https'),
querystring = require('querystring');
require('request-to-curl');
// First, get access token
var postData = querystring.stringify({
'grant_type': 'password',
'username':'<myusername>',
'password':'<mypassword>'
});
var options = {
hostname: 'myserver.com',
port: 443,
path: '/v1/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
function postVal(options, value) {
//var responseObject;
var access_token;
var responseObject;
var req = https.request(options, function(res)
{
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
console.log(responseString);
responseObject = (JSON.parse(responseString));
access_token = responseObject.access_token;
console.log('console access:' + access_token);
value(access_token);
// return responseObject;
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
}
console.log("Access:"+ postVal(options, function(access) {
console.log(access);
}));
Result:
$ node curl.js
Access:undefined
{"token_type":"Bearer","access_token":"f3DzDqW..dbMo0","refresh_token":"4jXqo4..kuuc2rMux3","scope":"global","access_token_type":"USER_GENERATED","note":"","expires_in":900}
console access:f3DzDqWpnrgxnxt5vE42ih8ew..gOKyJY5dbMo0
mlieber-ltm12:

node JS HTTP request does nothing using http request

I'm trying to send http post request using native node js http request.
I'm using the following code but nothing happens:
var http = require('http');
var options = {
hostname: '192.168.1.134',
port: '8082',
path: '/api',
method: 'POST',
headers: {'content-type': 'application/json',
'cache-control': 'no-cache'}
};
callback = function(response)
{
var result = [];
response.on('data', function (chunk)
{
result.push(chunk);
});
response.on('end', function ()
{
console.log("LOCAL END" + result);
});
}
var req = http.request(options, callback);
req.write(JSON.stringify(
{
customer: 'customer',
deviceIndicator: 'id',
userId: 'id2',
lastVersion: 999
}), 'utf8' ,
function(data)
{
console.log('flushed: ' + data);
});
req.end();
console.log(" - trying to post to example - done" );
But if i'm adding the following dummy calls i'm getting an answer from my local server as expected:
var options1 = {
hostname: 'www.google.com',
port: '80',
path: '/',
headers: {'cache-control': 'no-cache'}
};
callback1 = function(response1) {
var str = ''
response1.on('data', function (chunk) {
str += chunk;
});
response1.on('end', function () {
console.log("GOOGLE END" + str);
});
}
var req1 = http.request(options1, callback1);
req1.end();
console.log("sent to google - done");
What am i doing wrong?
Make sure 192.168.1.134:8082 is reachable and responding (using a browser, curl or wget) then try adding a content-length header:
var http = require('http');
var payload = JSON.stringify({
customer: 'customer',
deviceIndicator: 'id',
userId: 'id2',
lastVersion: 999
});
var options = {
hostname: '192.168.1.134',
port: 8082,
path: '/api',
method: 'POST',
headers: {
'content-length': Buffer.byteLength(payload), // <== here
'content-type': 'application/json',
'cache-control': 'no-cache'
}
};
var req = http.request(options, function(response) {
var result = [];
response.on('data', function(chunk) {
result.push(chunk);
});
response.on('end', function () {
console.log('LOCAL END' + result);
});
});
req.write(payload);
req.end();
Eventually, I discovered that the problem was with the device itself which had some kind of problem..
When sent http request to a direct ip address nothing happened but when sent to an address that need dns server it is working...
Unfortunately, I don't have any additional info about this bug...

Categories

Resources