如何在 http.put 请求中使用路径参数
How to use path parameters in http.put request
想知道在路径中发送参数的PUT
请求怎么写。例如,dev 将 PUT 请求的 URL 从使用查询字符串作为参数更改为使用路径中的参数。当参数作为查询发送时,我做了这样的事情:
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(…));
});
});
简单。
但是,现在 PUT 请求已更改为在路径中使用参数,即:
PUT api/products/customer/{customerId}/product/{productId}
我应该怎么写?
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);
以上可能是错误的,因为我不知道该怎么做。我很欣赏这个答案。谢谢。
你可以这样做:
this._$http.put(`api/products${customerId}/product/${productId}`, payload);
注意:我使用的是模板文字:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
谢谢!
已更新:
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(…));
});
});
想知道在路径中发送参数的PUT
请求怎么写。例如,dev 将 PUT 请求的 URL 从使用查询字符串作为参数更改为使用路径中的参数。当参数作为查询发送时,我做了这样的事情:
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(…));
});
});
简单。
但是,现在 PUT 请求已更改为在路径中使用参数,即:
PUT api/products/customer/{customerId}/product/{productId}
我应该怎么写?
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);
以上可能是错误的,因为我不知道该怎么做。我很欣赏这个答案。谢谢。
你可以这样做:
this._$http.put(`api/products${customerId}/product/${productId}`, payload);
注意:我使用的是模板文字:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
谢谢!
已更新:
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(…));
});
});