0

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.

3
  • 2
    The above is normal way... Commented May 21, 2019 at 16:30
  • @PetrAveryanov so my code is correct? Commented May 21, 2019 at 16:32
  • 2
    If it works - yes) I mean there is nothing complicated here - you can combine url in whatever way you like. If you use es6 you can use ${} notation like in answer below Commented May 21, 2019 at 16:34

1 Answer 1

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(…));
   });
});
Sign up to request clarification or add additional context in comments.

7 Comments

how would I add the payload -- as additional data is also required, but not as a parameter in the path.
@Chris22 I updated my answer. thank you and let me know if you have any more questions
I just updated it again, please take a look -- thank you!
@Chris22 Hi, You are welcome anytime! " Oh, one question, you are accessing the parameters off of the payload object even though they aren't defined in that object?" I have updated my answer which also includes the payload object. In this payload object : ` product_id: this.productId, customer_id: this.customerId,` are defined, or did you mean something else?
" should I put everything in the payload object instead of declaring them separately?" "so adding the payload object at the end makes me think that customer_id and product_id would be sent twice" So by putting in payload + using them in parameters it would be like sending twitce, 1 through parameter and 2nd through payload object. "is that correct or would it create an error?" Even if you send more than once it shouldn't give error in most cases. Thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.