Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django with IOS: getting multipart data (json + image) via POST

I'm new to both Django and IOS. I got stuck in this multipart data passing and need your advice!

Currently, I am working on an image-uploading function. From client side, I want to send the image file together with additional information (e.g. access_token). On the server side, I try to get the json data via request.raw_post_data and image via reuqest.FILES

It turns out that, I could only get either JSON data or Image, not both. What's worse, client side only returns 500 error with no other information...

Here's client 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 methods on the server side. one is with form which gave me 500 error

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

the other is like below (pls ignore the indentation problem)

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"
}

with the second method, I could get image uploaded but no access_token found. I'm wondering where the access_token has been stored -.-||| Or is it the problem with client side?

Your help is much appreciated!!!!

like image 710
xialin Avatar asked Nov 12 '22 08:11

xialin


1 Answers

it should be the problem of the server side. instead of getting from request.raw_post_data, the token information is actually in request.POST. so it would be like:

ck_token = request.POST['access_token'] 
like image 127
Sha Avatar answered Nov 15 '22 07:11

Sha