I want to send an Article from and Android client to a REST server. Here is the Python model from the server:
class Article(models.Model):
author = models.CharField(max_length=256, blank=False)
photo = models.ImageField()
The following interface describes the former implementation:
@POST("/api/v1/articles/")
public Observable<CreateArticleResponse> createArticle(
@Body Article article
);
Now I want to send an image with the Article data. The photo
is not part of the Article model on the Android client.
@Multipart
@POST("/api/v1/articles/")
public Observable<CreateArticleResponse> createArticle(
@Part("article") Article article,
@Part("photo") TypedFile photo
);
The API is prepared and successfully tested with cURL.
$ curl -vX POST http://localhost:8000/api/v1/articles/ \
-H "Content-Type: multipart/form-data" \
-H "Accept:application/json" \
-F "author=cURL" \
-F "photo=@/home/user/Desktop/article-photo.png"
When I send data through createArticle()
from the Android client I receive an HTTP 400
status stating that the fields are required/missing.
D <--- HTTP 400 http://192.168.1.1/articles/ (2670ms)
D Date: Mon, 20 Apr 2015 12:00:00 GMT
D Server: WSGIServer/0.1 Python/2.7.8
D Vary: Accept, Cookie
D X-Frame-Options: SAMEORIGIN
D Content-Type: application/json
D Allow: GET, POST, HEAD, OPTIONS
D OkHttp-Selected-Protocol: http/1.0
D OkHttp-Sent-Millis: 1429545450469
D OkHttp-Received-Millis: 1429545453120
D {"author":["This field is required."],"photo":["No file was submitted."]}
D <--- END HTTP (166-byte body)
E 400 BAD REQUEST
This is what is received as request.data
on the server side:
ipdb> print request.data
<QueryDict: {u'article': [u'{"author":"me"}'], \
u'photo': [<TemporaryUploadedFile: IMG_1759215522.jpg \
(multipart/form-data)>]}>
How can convert the Article object in a multipart conform data type? I read that Retrofit might allow to use Converters for this. It should be something that implements a retrofit.mime.TypedOutput
as far as I understood for the documentation.
Multipart parts use the
RestAdapter
's converter or they can implementTypedOutput
to handle their own serialization.
Related
- HTML 4.01 Specification - Form submission - multipart/form-data
- Retrofit Annotation Type Part documentation
- Upload multipart image data in JSON with Retrofit?
- REST - HTTP Post Multipart with JSON
- Retrofit Multipart Upload Image failed
- Retrofit issue #178: Create manual for sending files with retrofit
- Retrofit issue #531: Problem uploading file via POST/Multipart
- Retrofit issue #658: Not able to send string parameters with image when using Multipart
- Retrofit issue #662: Retrofit Form Encoded and Multipart in single request
According to your curl request you are trying to create smth like this:
With retrofit adapter this request can be created in a next way:
Usage:
Which creates similar output:
The key difference here is that you used POJO
Article article
as multipart param, which by default is converted byConverter
into json. And your server expects plain string instead. With curl you are sendingcURL
, not{"author":"cURL"}
.The server expects an "author" string but you're trying to pass it an "article" object. Pass it "String author" instead of "Article article."
Also, I think the "no file submitted" error is a red herring because the file is clearly present in your "request.data."