Parameter specification in a GET API REST request

I want to be able to specify the size of the product image in ONE request, I have different ideas about this, here they are:

mysuperstore.com/api/categories/40/products/53?width=100&height=100 

I think this is bad practice, because it is not clear what the width and height mean, maybe there is a physical size of the product.

Another variant:

 mysuperstore.com/api/categories/40/products/53/image?width=100&height=100 

This looks good, but in this case I need to make two requests, it looks like this is another resource identifier (image).

First product request

  mysuperstore.com/api/categories/40/products/53/ 

Second for image URL

 mysuperstore.com/api/categories/40/products/53/image?width=100&height=100 

Yes, I do not need to return the raw image (data), but just the URL.
I am creating an API on a PHP server using the Slim Framework. I found an example of such an API request with additional parameters

 $app->get('/archive(/:year(/:month(/:day)))', function ($year = 2010, $month = 12, $day = 05) use ($app) { echo sprintf('%s-%s-%s', $year, $month, $day); print_r($app->request()->get()); }); 

That way, the previous URL will match this example, and I can pass all the necessary parameters in a single request.

So my questions are: if this is good practice, perhaps this URL might be confused for those not familiar with the API.

  mysuperstore.com/api/categories/40/products/53/image?width=100&height=100 

Therefore, I ask someone more experienced in this than me, my goal is to create an API that can be understood clearly without reading tons of API documentation. And my API should follow all best practices.

That is why I ask this question, I hope someone can help me with this.

+6
source share
2 answers

I would do this:

 mysuperstore.com/api/categories/40/products/53/?image[width]=100&image[height]=100 
+3
source

I think that it is not connected with the SLIM base, but rather a matter of project implementation. You can always check out this API design best practice guide .

For your question about the image, in particular, you can follow the apple or the Google directive, they use image_name [WIDTH] x [HEIGHT] .jpeg, for example:

http://a4.mzstatic.com/us/r30/Purple7/v4/c2/36/25/c2362536-f6fc-4ef6-2a03-9b899ca00af9/screen480x480.jpeg

Images are pre-created and available on CDN. Depending on how many calls you have to process, but generating new images on the fly can be very busy. At the very least, you should store / cache the already generated images.

0
source

Source: https://habr.com/ru/post/985865/


All Articles