2

I'm working on an application where the URL/path to the API's CRUD functions are defined like so:

var newApiAddresses = {
        itemsGet: "<?php echo $view['router']->url('itembook_test_api_v1_apiitems_getitems'); ?>",
        itemAdd: "<?php echo $view['router']->url('itembook_test_api_v1_apiitems_itemadd'); ?>",
        itemEdit: "<?php echo $view['router']->path('itembook_test_api_v1_apiitems_itemedit', array('id' => null)); ?>",
        itemDelete: "<?php echo $view['router']->path('itembook_test_api_v1_apiitems_itemdelete', array('id' => null)); ?>"
    }

As expected the path for edit and delete receive an id. Now in my application I can create a post and get request with the urls defined in this manner like so:

  handlePostItemToApi () {
    axios.post(newApiAddresses.itemAdd, {
      "itemId": 1,
      "date": this.changeDateFormat(),
      ...

The above sends the POST request with no problem. But when I need to edit an item, I need to somehow include the itemid into this url. I tried doing it like so but it did not work

  handleEditItemToApi () {
    axios.put(newApiAddresses.itemEdit/{$id}, {
      "itemId": 1,
      "date": this.changeDateFormat(),
      ...

What is the correct way to include the id in the above url for put. I cannot hardcode the url like e.g. 'http://localhost/app_dev.php/api/v1/items/{$id}' as shown in axios documentation

jimiss
  • 119
  • 1
  • 1
  • 9
  • don't know if it's a typo but you're sending the `put` to `itemAdd` instead of `itemEdit` , try : `axios.put(newApiAddresses.itemEdit, { "itemId" : {$id}, "date" : ...` – Taki May 27 '18 at 07:07
  • @taki yes it is a typo – jimiss May 27 '18 at 07:08

1 Answers1

1

You can do it using the back quotes(not the usual single quote) in following way,

 axios.put(`newApiAddresses.itemEdit/{$id}`, {

or using the single quote as,

axios.put('newApiAddresses.itemEdit/'+id, {
Rohith Murali
  • 5,551
  • 2
  • 25
  • 26
  • Can you paste the request url that u can see from your network monitor – Rohith Murali May 27 '18 at 07:15
  • i don't know if this is the right one but here: REQUEST_URI "/app_dev.php/itemss/newApiAddresses.itemEdit/3" – jimiss May 27 '18 at 07:23
  • sorry this is the right one: REQUEST_URI "/app_dev.php/flights/newApiAddresses.itemEdit/%7B$id%7D" – jimiss May 27 '18 at 07:26
  • I dont think thats the right one, pls check again. If you can see the previously posted url, it must have worked. and the %7B$id%7D is the url encoded value of {$id} which must have gone before u made the change. Pls clear the log and reverify. – Rohith Murali May 27 '18 at 07:29
  • the previous one is from i added /id to the end. this is the one after I refreshed the page and edited a field. Sorry for the confusion – jimiss May 27 '18 at 07:32
  • Ok, can you paste the req url after u try with the second method – Rohith Murali May 27 '18 at 07:43
  • "/app_dev.php/items/newApiAddresses.itemEdit/%7B$id%7D" – jimiss May 27 '18 at 07:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171855/discussion-between-rohith-murali-and-jimiss). – Rohith Murali May 27 '18 at 07:52
  • so this the correct answer. The ' ' have to be removed. axios.put(newApiAddresses.itemEdit+id, { – jimiss May 27 '18 at 08:22