简体   繁体   English

如何向外部API发出GET和POST请求?

[英]How to make a GET and POST request to an external API?

var Attendance = require('../../../collections/attendance').Attendance;

var moment = require('moment');

module.exports = function(app) {

app.get('/api/trackmyclass/attendance', function(req, res) {
    var data = req.body;
    data['user'] = req.user;
    Attendance.getByUser(data, function(err, d) {
        if (err) {
            console.log('This is the err' + err.message);
            res.json(err, 400);
        } else {
            var job = d['attendance'];
            if (typeof job != undefined) {
                res.json(job);
                console.log('This is it' + job['status']);
            } else
                res.json('No data Present', 200);
        }

    });
});

app.post('/api/trackmyclass/attendance', function(req, res) {
    var data = req.body;
    data['user'] = req.user;
    Attendance.create(data, function(err, d) {
        if (err) {
            console.log('This is the err' + err.message);
            res.json(err, 400);
        } else {
            var attendance = d['attendance'];
            if (typeof job != undefined) {
                console.log('Attendance record created' + attendance);
                res.json(attendance);
            } else
                res.json('No data Present', 200);
        }

    });
});
}

This is the api code I to which I need to make the GET and POST request. 这是我需要向其发出GET和POST请求的api代码。 But I have no idea how to do it. 但是我不知道该怎么做。

It looks like your code is using express which would normally be good for building and API for your app. 看起来您的代码正在使用express,这通常对于构建应用程序和API很有用。 However to make a simple request to a third party api and staying in node.js why not try the request module which is great. 但是,要向第三方api发送简单请求并保留在node.js中,为什么不尝试使用很棒的请求模块。 https://www.npmjs.org/package/request https://www.npmjs.org/package/request

Your example does not show what the path of the request is or if you need any additinal headers etc but here is a simple example of a GET request using request. 您的示例未显示请求的路径是什么,或者是否需要任何附加标头等,但这是使用request的GET请求的简单示例。

var request = require('request');

function makeCall (callback) {
    // here we make a call using request module
    request.get( 
        { uri: 'THEPATHAND ENDPOINT YOU REQUEST,
         json: true,
          headers: {
            'Content-Type' : 'application/x-www-form-urlencoded',
        }
        },
        function (error, res, object) {
          if (error) { return callback(error); }

            if (res.statusCode != 200 ) {
              return callback('statusCode');
            }

            callback(null, object);
        }
      );

}

or jquery .ajax from a front end client direcct to your path 或jquery .ajax从前端客户端定向到您的路径

$.ajax({
url: "pathtoyourdata",
type: "GET",
})
.done(function (data) {
//stuff with your data
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM