user7623748
user7623748

Reputation: 71

Cannot GET on POST request

I have an API that takes in 3 url parameters and writes into a mongodb collection. My understanding is that it should be post request since it's sending data to my back end, but when sending the data in, I get a Cannot Get... error.

routes.js

// JavaScript source code
var friends = require('./../controllers/friends.js');

module.exports = function(app)
{
    app.post('/friends/new/:fname/:lname/:bday', function (request, response)
    {
        friends.create(request, response);
    })
}

Controller:

// JavaScript source code
var mongoose = require('mongoose');
var Friend = mongoose.model('Friend');

module.exports = 
{
    create: function(request, response)
    {
        var friendInstance = new Friend();
        friendInstance.first_name = request.params.fname;
        friendInstance.last_name = request.params.lname;
        friendInstance.b_day = request.params.bday;
        friendInstance.save(function(err)
        {
            if (err)
            {
                response.josn(err);
            }
}

URL:

http://localhost:8000/friends/new/Gelo/Maverick/9999-9-99

Error:

Cannot GET /friends/new/Gelo/Maverick/9999-9-99

Upvotes: 1

Views: 3633

Answers (1)

Pat Needham
Pat Needham

Reputation: 5928

You're seeing that error because you must be trying to access the url via the web browser, which sends the GET request. You should use an app like Postman to make POST requests instead.

Upvotes: 2

Related Questions