Tôi muốn gửi Điều từ và Android ứng dụng khách đến máy chủ REST. Đây là Python mô hình từ máy chủ:Làm cách nào để gửi nhiều dữ liệu/biểu mẫu với Retrofit?
class Article(models.Model):
author = models.CharField(max_length=256, blank=False)
photo = models.ImageField()
Giao diện sau đây mô tả việc thực hiện trước đây:
@POST("/api/v1/articles/")
public Observable<CreateArticleResponse> createArticle(
@Body Article article
);
Bây giờ tôi muốn gửi một hình ảnh với Điều dữ liệu. photo
không phải là một phần của mô hình Điều trên máy khách Android của khách hàng.
@Multipart
@POST("/api/v1/articles/")
public Observable<CreateArticleResponse> createArticle(
@Part("article") Article article,
@Part("photo") TypedFile photo
);
API được chuẩn bị và thử nghiệm thành công với cURL.
$ curl -vX POST http://localhost:8000/api/v1/articles/ \
-H "Content-Type: multipart/form-data" \
-H "Accept:application/json" \
-F "author=cURL" \
-F "[email protected]/home/user/Desktop/article-photo.png"
Khi tôi gửi dữ liệu thông qua createArticle()
từ Android client tôi nhận được một tình trạng HTTP 400
nói rằng các lĩnh vực được yêu cầu/thiếu.
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
Đây là những gì được nhận như request.data
ở phía máy chủ:
ipdb> print request.data
<QueryDict: {u'article': [u'{"author":"me"}'], \
u'photo': [<TemporaryUploadedFile: IMG_1759215522.jpg \
(multipart/form-data)>]}>
Làm thế nào có thể chuyển đổi các Điều đối tượng trong một nhiều phần dữ liệu phù hợp với kiểu dữ liệu? Tôi đọc rằng Retrofit có thể cho phép sử dụng Converters cho việc này. Nó phải là một cái gì đó mà thực hiện một retrofit.mime.TypedOutput
theo như tôi hiểu cho documentation.
Bộ phận đa phần sử dụng bộ chuyển đổi
RestAdapter
hoặc có thể triển khaiTypedOutput
để xử lý tuần tự của riêng chúng.
liên quan
- 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
Đây không phải là lớp TypedFile có thể được sử dụng cho điều này? – user2511882
Phương pháp của bạn có vẻ ổn. Tại sao bạn không cho phép đăng nhập vào 'RestAdapter' và kiểm tra chính xác dữ liệu nào đang được gửi đi. – corsair992
@ user2511882 Bạn có nghĩa là tôi nên sử dụng 'TypedFile' cho cả dữ liệu JSON (' bài viết') và hình ảnh? Vui lòng chỉ cho tôi cách chuyển đổi dữ liệu./@ corsair992 Tôi đã cập nhật bài đăng của mình. – JJD