tale
tale

Reputation: 15

Avoiding http error 405 while executing an put command with angularjs

First to say, when I enter the put command in POSTMAN (its then a POST there) I have no problems. Trying to execute the PUT command in code I receive a http 405 error.

The exact error is:

<p>Problem accessing /api/portfolio/createTransaction. Reason:
<pre>    Method Not Allowed</pre></p><hr>

This is my code:

const app = angular.module('demoAppModule', ['ui.bootstrap']);
const apiBaseURL = "/api/portfolio/";

console.log(apiBaseURL);

app.controller('DemoAppController', function($scope, $http) {

    const demoApp = this;

    demoApp.createTransaction = function () {

        console.log("Initiating createTransaction...")

        var transactionDetails = $.param({
                Reference: $scope.reference,
                Quantity: $scope.quantity,
                TradeDate: $scope.tradeDate,
        });

        const createTransactionURL =
            apiBaseURL
            + "createTransaction";

        console.log("Executing flow");
        console.log("Transaction URL = " + createTransactionURL);
        console.log("Transaction Details = " + transactionDetails);

        $http.put(createTransactionURL, angular.toJson(transactionDetails))
        .then(function (transactionDetails, status, headers){
            $scope.ServerResponse = transactionDetails;
        })
        .catch(function (transactionDetails, status, header, config) {
            $scope.ServerResponse =  htmlDecode("transactionDetails: " + transactionDetails +
                "\n\n\n\nstatus: " + status +
                "\n\n\n\nheaders: " + header +
                "\n\n\n\nconfig: " + config);
        });

    };

});

Appreciate any help!

Upvotes: 0

Views: 32

Answers (1)

Igy
Igy

Reputation: 43816

The server is rejecting your request, likely because it doesn't allow PUT requests. It's probably expecting a HTTP POST instead of a PUT.

If so, change $http.put to $http.post - the method signatures are identical.

If you think the server should be accepting a PUT, you'll need to check why the server side code is rejecting it, which won't be apparent in the client code

Upvotes: 1

Related Questions