I've been working on creating a client for GroupMe using their provided API, but I'm stuck and can't seem to figure out what's going wrong.
curl -X POST -H "Content-Type: application/json" -d '{"message": { "text": "Nitin is holding me hostage", "source_guid": "7374"}}' https://api.groupme.com/v3/groups/30885833/messages?token=I_PUT_MY_ACCESS_TOKEN_HERE
After running the above command, it successfully returns:
Click here to see JSON (Hastebin)
The problem arises when I try to incorporate this into Javascript code. Here's what I have so far:
var HTTPS = require('https');
var request = require('request');
function postMessage() {
var options, body, botReq;
options = {
hostname: 'api.groupme.com',
path: '/v3/groups/30885833/messages?token=DbZoE9Eablg43ZIGdfKsFkXDjLzR6RDUkwHT9JNn',
method: 'POST'
};
body =
{ '"message"': { '"text"': "I am a post message", '"source_guid"': "7374" } };
console.log(body);
botReq = HTTPS.request(options, function (res) {
if (res.statusCode == 201) {
//neat
} else {
console.log('rejecting a bad status code ' + res.statusCode);
}
});
botReq.on('error', function (err) {
console.log('error posting message ' + JSON.stringify(err));
});
botReq.on('timeout', function (err) {
console.log('timeout posting message ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
However, when I run this script, I only get an Error Code of 400 and I'm not sure how to pinpoint the issue causing the Bad Request
.
Can someone guide me on how to correctly transform the initial command into functional javascript code? Any help would be appreciated! Thanks!