How to use path parameters in http.put request

≡放荡痞女 提交于 2019-12-11 15:23:18

问题


I want to know how to write the PUT request that sends parameters in the path. For example, dev changed the URL for the PUT request from a using a query string as parameters to using parameters in the path. When params were sent as query, I did something like this:

let payload = {
   product_id: this.productId,
   customer_id: this.customerId,
   userGuide_id: this.userGuide
}

return this._$q((resolve, reject) => {
   this._$http.put(‘/api/products/customer/mostRecent’, payload)
   .then((result) => resolve(result))
   .catch((err) => {
      reject(…));
   });
});

Easy.

However, now that the PUT request is changed to use params in the path i.e.:

PUT api/products/customer/{customerId}/product/{productId}

How exactly would I write that?

let customer_id = this.customerId,
    product_id = this.productId;

let payload = {
    user_GuideId: this.userGuideId
}

this._$http.put(“api/products/”+customer_id+“/product/”+product_id, payload);

The above is probably wrong since I don't know how to do this. I appreciate the answer. Thanks.


回答1:


You can do something like this:

this._$http.put(`api/products${customerId}/product/${productId}`, payload);

Note: I am using Template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Thank you!

Updated:

let payload = {
   product_id: this.productId,
   customer_id: this.customerId,
   userGuide_id: this.userGuide
}

return this._$q((resolve, reject) => {
   this._$http.put(`api/products${payload.customer_id}/product/${payload.product_id}`, payload);
   .then((result) => resolve(result))
   .catch((err) => {
      reject(…));
   });
});


来源:https://stackoverflow.com/questions/56242613/how-to-use-path-parameters-in-http-put-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!