Z
Z
zasqer2015-03-12 21:02:39
Angular
zasqer, 2015-03-12 21:02:39

How to upload a file along with data via REST API?

Some context: There is a server with a REST API and a client - AngularJS. I'm making a feature "create a product in the catalog" and I want to upload an image when creating this product. Before that, I only loaded pictures the old way - submitting a form and receiving files from multipart/form-data.
How to upload files and data following REST principles? Do I need to transfer the image and data in one request or two separate ones?
For example, send separate requests - a picture with content-type: image/jpeg, and data - application/json.
Or, for example, together - pack everything in multipart / form-data, and json as a string in form-data?
What are the best practices specific to AngularJS? How does file upload fit with $resource or restangular?
If this is important - the server is based on Python Flask (I think it's not important), and the business logic is as follows - a product can be created without a picture, but a picture cannot be created without a product.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-03-12
@miraage

In my opinion, the picture is a separate entity.
And make a has_one/has_many relationship in the product and belongs_to in the picture. For example, the paperclip
gem .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question