Nodejs restful API无法POST

时间:2016-10-05 13:06:29

标签: node.js rest express

我已经使用在redis上运行的nodejs创建了一个REST API,我可以使用get方法调用api并且它工作正常但是当我将其更改为post并尝试使用post调用它时。我收到错误Cannot POST。我在下面附上我的代码,你能不能让我知道我在做什么错误

app.post('/foobar/:ticketid/:date/:from_time/:to_time/:seats', function(req, res) {
    var id = req.params.ticketid;
    var date = req.params.date;
    var seats = req.params.seats;
    var from_time = req.params.from_time;
    var to_time = req.params.to_time;
    var key = id + date;
    var result = {};

    client.exists(key, function(err, reply) {
        if (reply === 1) {
            //console.log("Key Found data from key: ");
            client.get(key, function(err, reply) {
                var output = JSON.parse(reply);
                var data = [];
                output.forEach(function(entry) {
                    var temp = {};
                    temp['from_time'] = entry["from_time"];
                    temp['to_time'] = entry["to_time"];
                    if ((entry["from_time"] == from_time) && (entry["to_time"] == to_time)) {

                        temp['vacancies'] = entry["vacancies"] - seats;
                    } else {
                        temp['vacancies'] = entry['vacancies'];
                    }
                    data = data.concat(temp);
                });
                client.set(key, JSON.stringify(data));


                result['response'] = 1;
                result['message'] = 'vacancies successfully updated!';
                res.json(result);
            });
        } else {
            result['response'] = 0;
            result['message'] = 'Invalid timeslot!';
            res.json(result);
        }
    });
});

我正在使用邮递员和Rest easy插件进行呼叫。 在我的网址http://xxx.xxx.xxx.xxx:8081/foobar/ 在我体内,我正在设置键值对

邮递员代码:

POST /foobar/ HTTP/1.1
Host: xxx.xxx.xxx.xxx:8081
Authorization: Basic NDM5OlA5MS1RVDhQLUs4RzktTDU2
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: ab3cb383-62d4-475f-0e59-881a167156eb

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="ticketid"

251
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="date"

2016-05-14
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="from_time"

18:00
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="to_time"

21:00
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="seats"

5
------WebKitFormBoundary7MA4YWxkTrZu0gW--

2 个答案:

答案 0 :(得分:1)

如果要在正文中发布值,则无需在URL中列出它们。只需确保您的标头设置为"Content-Type": "application/json"

app.post('/foobar', function(req, res) {
    var id = req.body.ticketid;
    var date = req.body.date;
    var seats = req.body.seats;
    var from_time = req.body.from_time;
    var to_time = req.body.to_time;
    ....
}

答案 1 :(得分:0)

这是传递参数并获取参数的正确方法

试试这种方式

app.post('/foobar/ticket/:ticketid/date/:date/from_time/:from_time/to_time/:to_time/seats/:seats

以这种方式从客户端发布

/foobar/ticket/123/date/your date/from_time/your date/to_time/your time/seats/123234

并且像这样得到它

使用req.params

检查所有参数
var id = req.params.ticketid;
var date = req.params.date;
var seats = req.params.seats;
var from_time = req.params.from_time;
var to_time = req.params.to_time;
var key = id + date;