如何在http.put请求中使用路径参数 [英] How to use path parameters in http.put request

查看:163
本文介绍了如何在http.put请求中使用路径参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何编写在路径中发送参数的 PUT 请求.例如,开发人员将PUT请求的URL从使用查询字符串作为参数更改为在路径中使用参数.当将参数作为查询发送时,我做了这样的事情:

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(…));
   });
});

容易

但是,现在PUT请求已更改为在路径中使用参数,即:

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

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);

以上可能是错误的,因为我不知道该怎么做.我很欣赏答案.谢谢.

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

推荐答案

您可以执行以下操作:

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

注意:我正在使用模板文字: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals

Note: I am using Template literals: 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(…));
   });
});

这篇关于如何在http.put请求中使用路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆