Danny Sullivan
Danny Sullivan

Reputation: 3844

URL design for GET resource in REST API for images

I have a Rest API that allows users to POST to an endpoint to upload a photo:

POST /api/photos

The response would be as follows:

{
  "id": "1",
  "title": "my image"
  "url": "https://some_s3_url"
}

The user can then access the photo metadata through the GET endpoint

GET /api/photos/1

What is the best way to model a url for getting the content of the image url? Note that I'd like to ensure the user has the ability to access this image, so requesting the s3 bucket url directly will not work here.

I have considered doing something like:

GET /api/photos/1/content

Though that seemed hacky

Upvotes: 0

Views: 1729

Answers (1)

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

first of all POST request should not return response, it could return header called location to return URI of created resource for ex. http://youer-server/api/photos/1 then in GET request for this URL you can return the json object as:

{
  "id": "1",
  "title": "my image",
  "url": "https://some_s3_url",
  "base64": .....
}

then you can encode your image as base64 and provide its content in the same object.

Upvotes: 1

Related Questions