如何使用请求模块NodeJs表达发布请求

时间:2017-11-21 10:19:32

标签: node.js api post request

如何向此端点发出正确的帖子请求。当我使用POSTMAN时,我得到正确的响应但是当我使用下面的函数调用时,我得到503错误。根据我的说法,电话似乎很好。我感谢你的帮助!!

const request = require('request');
const express = require('express');

// Initialize request
var img64Data = "/9j/4AAQSkZJRgABAQAAAQABAAD/2w… "; // Include the entire base64 encoding. // Shown Below in the next page 
var send = {"img64": img64Data};
var api_address = "https://8n78hbwks0.execute-api.us-west-2.amazonaws.com/dev/";

// Make Post Request

module.exports = app => {
  app.post('/axe', (req, res, next) => {
    console.log("inside the axe");
    request.post({
      url: api_address,
      body: JSON.stringify(send),
      headers: {"Content-Type":"application/json"}
    }, function (error, response, body) {
      console.log("hiii");
      console.log(response.statusCode);
      if (!error && response.statusCode == 200) {
        // Successful call
        var results = JSON.parse(body);
        console.log(results) // View Results
      }
    });
  });
};

1 个答案:

答案 0 :(得分:0)

您收到503错误https://en.wikipedia.org/wiki/List_of_HTTP_status_codes,因为您的服务器没有回复任何http代码。

if (!error && response.statusCode == 200) {
  // Successful call
  var results = JSON.parse(body);
  console.log(results) // View Results
  res.sendStatus(200);
} else {
  res.sendStatus(response.statusCode);
}