-3

So I've been researching this for days and have not been able to find the answer online. I'm creating an API in AWS API Gateway and noticed the difference between 'path parameters' and 'query parameters'. I already read and know the differences, when each of them should be used, etc. However, what I can not find is HOW to implement and send the path parameters?

For example, when it comes to query parameters you can send those in the 'data' part of ajax jQuery. Easy. But again, how can I include the path parameters in that ajax call? I can set the path on API GATEWAY but not sure how to send the data for it from the front end. Any help would be greatly appreciated. Thanks.

Example: root/{Animal}/

How to send the "animal" i.e. 'dog' as a path parameter instead of a query parameter? i.e. /?animal=dog

  • 3
    It might be helpful if you were to able to share some code you've written so far for this ajax request and if you could describe the expected flow of information from the front end a bit more so the community can provide a more meaningful answer. – Katrpilar Sep 28 '19 at 00:21
  • I edited the question with an example, except the ajax part since I don't think it would help. – UltimateTechDiscovery Sep 28 '19 at 01:28

1 Answers1

1

You just need to form the URL string, and don't send any data with that.

let animal = 'dog';

$.ajax({ 
url: serviceUrl + '/' + animal,
method: 'GET',
success: function(res) { // your code }
});

If you are taking input from a textbox with id txtSearchAnimal:

let animal = $('#txtSearchAnimal').val();

And use the above ajax call.

Ashmah
  • 842
  • 2
  • 8
  • 17
  • Thanks for the answer. So is that the "official" way of sending path parameters from JavaScript? It looks like more "hard-coded" as opposed to looking cleaner. Like for example, how 'data' can be used to send query parameters, etc. But thanks though. One question, can I still send query parameters with 'data' as well as that URL with its path parameters as well? Is it ok to send both if needed? – UltimateTechDiscovery Sep 28 '19 at 02:56
  • `can I still send query parameters with 'data' as well as that URL with its path parameters as well? Is it ok to send both if needed?` - Yes. You can send both. According to REST API guidelines path parameters are the ways to structure URLs, but we can use both. The general idea is to keep required parameters as path parameters and query params as optional. For your ref: https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam – Ashmah Sep 28 '19 at 03:08
  • `So is that the "official" way of sending path parameters from JavaScript? It looks like more "hard-coded" as opposed to looking cleaner` - Its just an example. I have also mentioned if you have a search text box and you want to set the path params according to user input, you can use that. If you want it from a config, you just need the config value pushed to the variable (here its animal, but can be anything) – Ashmah Sep 28 '19 at 03:11
  • 1
    @UltimateTechDiscovery "hard coded as opposed to looking cleaner"? How is string concatenation with two variables in any way hard coded? Cleaner than what? You could use interpolation instead of concatenation `${serviceUrl}/${animal}` maybe that's cleaner but what is it your were looking for? – Jared Smith Sep 28 '19 at 12:01
  • I meant cleaner as in something that exist that does that functionality instead of 'hard coding' the data into the URL. For example, to send data with query parameters, you don't 'hard code' the serialized string into the URL like literally writing "?cat=Chloe&dog=Marley" etc., instead there's a cleaner way of doing it with ajax 'data' i.e. data: serialize(xvariable);. See the difference? I was wondering if there was an official cleaner way of doing the path parameters that for example does what data in ajax does but for path parameters; instead of literally writing it on the URL. – UltimateTechDiscovery Sep 28 '19 at 14:21
  • @UltimateTechDiscovery how would you do that? Since path parameters are ordered you'd have to have them in an array, and then it's just `["foo", "bar"].join("/")`. Variables holding strings work just as well as string literals. I get why you need a specialized function to encode a query string (and indeed have written them), but I don't see why you'd need one here. It does make more sense for actual filepaths which could have a different delimiter (i.e. Windows filepaths) and indeed node.js has a function to do just that. – Jared Smith Sep 30 '19 at 12:35