Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 598
thank_you Avatar asked Nov 10 '22 06:11

thank_you


1 Answers

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

like image 146
Giovanni Vinaccia Avatar answered Nov 15 '22 06:11

Giovanni Vinaccia