Django with iOS: getting multi-page data (json + image) via POST

I am new to Django and iOS. I am stuck with this multi-data transfer and need your advice!

I am currently working on an image upload function. On the client side, I want to send an image file along with additional information (for example, access_token). On the server side, I am trying to get json data through request.raw_post_data and image through reuqest.FILES

Turns out I could only get JSON or Image data, not both. Worse, the client side returns only 500 errors without any other information ...

Here is customer code

NSURL *url = [NSURL URLWithString:urlPath];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

// The parameter to send
NSDictionary * params = dictionaryToSent;

// the image need to upload
NSData *imageData = UIImageJPEGRepresentation(image, 1);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData
     appendPartWithFileData:imageData
     name:@"image"
     fileName:@"uniqueFileName"
     mimeType:@"image/jpeg"];
}];

AFJSONRequestOperation* jsonOperation=[[AFJSONRequestOperation alloc]initWithRequest:request];

[jsonOperation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];

[jsonOperation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id JSON) {

   // Handler for request is completed    
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //Handler for request is failured;
}];

[jsonOperation start];

And I tried two server side methods . one of them has a form that gave me 500 errors

form = UploadFileForm(request.POST, request.FILES)

, (pls )

data=json.loads(request.raw_post_data)
ck_token = data['access_token']
if 'image' in request.FILES:
upload = request.FILES['image']
filename = upload.name
user = Basic.objects.get(ck_token = ck_token)
post = Post(user_id = user.user_id, image = upload, caption = "Hello World")
post.save()
ret_json = { 
    'success': True, 
    'post_id': post.id
}
else:
ret_json = { 
    'success': False,
    'error_message': "image not found"
}

, , access_token. , access_token -.- ||| ?

!!!!

+5
1

. request.raw_post_data request.POST. :

ck_token = request.POST['access_token'] 
+1

All Articles