Here is my http POST code, running on localhost:
if(headers['Content-Type'] == undefined)
headers['Content-Type'] = 'application/x-www-form-urlencoded';
var post_options = {
host: host,
path: path,
port: port,
method: 'POST',
headers: headers
};
if(headers['Content-Type'] == "application/json"){
post_options["json"] = true;
var post_data = JSON.stringify(body);
}else
var post_data = querystring.stringify(body);
var post_req = http.request(post_options, function(res) {
var body = '';
console.log("INSIDE CALLBACK HTTP POST");
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
console.log('Response: ' + chunk);
});
res.on('end', function () {
var post = querystring.parse(body);
console.log("FINAL BODY:",post);
});
//console.log("RESPONSE in http POST:",res);
});
// post the data
console.log("WRITING HTTP POST DATA");
var sent_handler = post_req.write(post_data);
console.log("POST_REQ:",post_req);
console.log("sent_handler:",sent_handler);
post_req.end();
Here is the information I send to instagram exactly:
- host = "api.instagram.com"
- path = "/oauth/access_token"
-
body as follows:
body["client_id"] = CLIENT_ID;
body["client_secret"] = CLIENT_SECRET;
body["grant_type"] = "authorization_code";
body["redirect_uri"] = AUTHORIZATION_REDIRECT_URI;
body["code"] = login_code;
body["scope"] = "public_content";
-
headers = {} (empty, assumes headers['Content-Type'] == undefined as true)
-
Important: sent_handler returns false
-
The console.log for "FINAL BODY" (variable post) returns "{}"
To note: communications using curl with the api instagram works. So I really believe the problem is in some part of this code in nodejs.
Does anyone have any idea? Please ask if there is need for more information