thank_you
thank_you

Reputation: 11107

PUT request sending params via URL

I have a order resource that looks like so.

.factory('Order', order)

order.$inject = ['$resource', "ApiEndpoint"];

function order($resource, ApiEndpoint) {
  return $resource(ApiEndpoint.url + 'orders.json', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
    update: {method: 'PUT'},
    edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
    remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
  });
}

When I run Order.update like so

var params = {
  order: {
    line_items_attributes: {0: {quantity: 2, id: 1}}
  },
  order_id: 3
};

Order.update(params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

I also tried this:

Order.update({}, params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

The params sent to the server end up being inside the URL. For example this was one of the urls the server received

path="/api/mobile/orders.json?order=%7B%22line_items_attributes%22:%7B%220%22:%7B%22quantity%22:8,%22id%22:356265%7D%7D%7D"

I should also note that the method being received by the server is an OPTIONS request. The server has been set up to handle this.

Since I'm sending a PUT request why is $resource delivering the params via the URL and not part of the payload?

Upvotes: 5

Views: 2250

Answers (2)

maddygoround
maddygoround

Reputation: 2290

If you are updating an order then you should speficy the order id so that service can now which order is going to be updated

function order($resource, ApiEndpoint) {
  return $resource(ApiEndpoint.url + 'orders.json/:orderid', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
    update: {method: 'PUT',params : {orderid : '@order_id'},
    edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
    remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
  });
}

and then your call

Order.update(params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

Upvotes: 0

Giovanni Vinaccia
Giovanni Vinaccia

Reputation: 21

from the docs:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

the payload is the second argument, so try with this code

var params = {
  order: {
    line_items_attributes: {0: {quantity: 2, id: 1}}
  },
  order_id: 3
};

Order.update({}, params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

just add an empty object as first parameter to the update method.

have also a look to he section related to custom put requests

Upvotes: 1

Related Questions