问题
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