Example scenario,
API endpoint for a Product with Product information as JSON data and there can be 0 to n number of binary Images.
Currently we upload as Binary file, with both Image and JSON data through /v1/products POST method.
Is this right way? or what are other approaches I can try out, to make a more simple Restful endpoint.
so once a retrieve data using GET /v1/products/:productID
{ "id":"123", "name":"product name", imgUrls:["pathToImage1","pathToImage2"]}
Advantage:
- Single API Call
Disadvantage:
- Product actually need Image URL not binary files itself, POST and GET payload looks different, am I deviating REST principles?
- As Binary files are getting uploaded, it is not easy to do a POST from simple Rest tools like POSTMAN or similar tools
Alternate approach
Upload Images separately and get the Image URL and later on Product payload, refer the URL
Advantages:
- GET and POST API looks the same
- Simple to invoke POST API from POSTMAN and others
Disadvantages:
- Multiple API call.
1 Answer 1
Use the second approach, as image data is a completely different media type than JSON application data. The second API call doesn't matter at all in this context but you gain a lot of cleanliness and stability. In addition, if you separate the application RESTful service from the media service which only serves static data you may be able to scale much easier.