Я пытаюсь загрузить изображение из моего клиентского приложения для iOS на внутренний сервер Django. Вот что я делаю на клиенте:
func submitPhotoAndInfo(photo: UIImage, info: Info, completionHandler: @escaping (Bool, String?) -> Void) {
let headers = ["Authorization": "Token " + self.accessToken!, "Content-type": "multipart/form-data"]
self.showNetworkActivity(active: true)
Alamofire.upload(multipartFormData: { multipartFormData in
do {
let dict = try info.asDictionary()
for (key, value) in dict {
if value is String || value is Int {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
}
} catch let error as NSError {
completionHandler(false, error.localizedDescription)
}
let imageData = UIImageJPEGRepresentation(photo, 0.8)
multipartFormData.append(imageData!, withName: “my_photo", fileName: “my_photo.jpg", mimeType: "jpg/png")
}, usingThreshold: UInt64.init(), to: "https://myapp.herokuapp.com/users/submit_photo_and_info", method: .post, headers: headers) { encodingResult in
self.showNetworkActivity(active: false)
switch encodingResult {
case .success(let response, _, _):
if response.response?.statusCode == 200 {
completionHandler(true, nil)
} else {
completionHandler(false, "Server error")
}
case .failure(let error):
print(error)
completionHandler(false, error.localizedDescription)
}
}
}
И это на сервере Django:
@api_view(['POST'])
@parser_classes((MultiPartParser,))
@authentication_classes((TokenAuthentication,))
def submit_photo_and_info(request):
file_obj = request.data[‘photo']
reg = Info()
reg.user = request.user
reg.photo = request.data[‘photo']
reg.status = 'aw'
try:
resp = requests.get(“photo")
resp.raise_for_status()
except Exception as e:
logging.error(e)
return Response({}, status=status.HTTP_406_NOT_ACCEPTABLE)
image_file = ContentFile(resp.content)
reg.photo.save(str(user.id) + ".jpg", image_file)
reg.save()
return Response({}, status=status.HTTP_200_OK)
Кажется, что-то не так, потому что я получаю эти ошибки на сервере:
Aug 28 13:58:28 myapp app/web.1: 10.35.220.176 - - [28/Aug/2018:13:58:27 -0700] "POST /users/submit_photo_and_info HTTP/1.1" 301 0 "-“ "MyApp/0.1 (com.myapp.MyApp; build:25; iOS 11.4.1) Alamofire/4.7.2"
Aug 28 13:58:28 myapp heroku/router: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/users/submit_photo_and_info" host=myapp.herokuapp.com request_id=358902be-53e8-4a49-a96e-34d6a45a2b95 fwd="23.242.1.246" dyno=web.1 connect=1ms service=510ms status=503 bytes=220 protocol=https
Есть идеи?