M
M
megamutex2019-04-23 15:15:38
API
megamutex, 2019-04-23 15:15:38

Why is node.js not POSTing?

Good afternoon!
There is a small test application on node.js with the express.js framework.
I'm trying to test the Amazon Moments API https://developer.amazon.com/docs/moments/rewards-...
When I try to make a POST request, I get this in response:
5cbeff34d591f580090997.png
Start index.js :

var express = require('express');
var amaz = require('./amazon.js');
var app = express();
var PORT = process.env.PORT || 80;

app.use(express.static(__dirname + "/public"));

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(PORT,function(){
  console.log('server successfully started on port ' + PORT);
  amaz;
});

Amazon.js:
const https = require('http')

const data = JSON.stringify({
  "x-api-key": "12345",
  "Content-Type": "application/json",
  "appId": "DEMO1",
  "momentId": "GAME_COMPLETE",
  "deviceType": 'Android ',
  'campaignId ': "DEMOCAMP1",
  "rewardGroupId": "amz1yprime"
})

const options = {
  url: 'https://dnxr7vm27d.execute-api.us-east-1.amazonaws.com/prod/GetRewardInfo',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-api-key': 'kE2xi2OgUa7jfijmsd0jQ74aJntJwUEW2EU8LUsi'
  }
}

const req = https.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  module.exports = res.on('data', (d) => {
    console.log(d);
  })
})

req.on('error', (error) => {
  console.error(error.message)
})

req.write(data)
req.end()

Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
megamutex, 2019-04-24
@megamutex

I decided to remake the code for the Request module without using the Http module:

const request = require('request')

var options = {
  url: 'https://dnxr7vm27d.execute-api.us-east-1.amazonaws.com/prod/GetRewardInfo',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
    'x-api-key': 'kE2xi2OgUa7jfijmsd0jQ74aJntJwUEW2EU8LUsi'
  },
  body: {
    'appId': 'DEMO1',
    'momentId':'GAME_COMPLETE',
    'deviceType' : 'Android',
    'campaignId' : 'DEMOCAMP1',
    'rewardGroupId': 'amz1yprime'
  },
  json: true // sets body to JSON representation of value 
};

request.post(options, (err, httpResponse, body) => {
  if (err) console.error(err);
  // httpResponse contains the full response object, httpResponse.statusCode etc
  else console.log(body);
})

Everything worked as it should

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question