RESTframework icon indicating copy to clipboard operation
RESTframework copied to clipboard

Post file and extra parameters

Open olkarls opened this issue 12 years ago • 7 comments

Hi Ivan!

Thank you for the framework.

How can I add upload a file and additional parameters to the same request?

I've got the following code:

RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:BASE_URL] type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeMultiPartFormData resourcePathComponents:token, @"resource", nil];

NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/tempImage.jpg"];    
[r addData:[NSData dataWithContentsOfFile:jpgPath] withContentType:@"image/jpeg" forKey:@"image.jpg"];

[r addParam:@"Some String" forKey:@"resource[string_value]"];
[r addParam:@"2012-06-10" forKey:@"resource[another_string_value]"];

Only one parameter is sent to the Rails API.

What am I missing?

Thanks

/ Ola

olkarls avatar May 23 '12 09:05 olkarls

Hey Ola,

Haven't tried your example but it looks cool on the first look. You say only 1 param gets through, which one? Can you try setting a breakpoint here: https://github.com/ivasic/RESTframework/blob/master/RESTframework/RESTframework/RFClasses/RFRequest.m#L265 and follow the flow with the debugger see if some of the params are skipped and let me know if I can help further.

But yes, your logic is all good and this is the correct way of adding files and key/value params.

Cheers

ivasic avatar May 23 '12 17:05 ivasic

I'm facing the same problem. I wanna send (HTTP POST) a png file and other string parameters. But when I post it, every parameter has a string concat "ThIs_Is_tHe_bouNdaRY_$".

I only wanna send param = value and it is sending a crazy set of data. Can you help me?

Log of my sever:

2012-06-13 21:06:29 --- INFO: **** **** **** KLOG START **** **** **** **** 2012-06-13 21:06:29 --- INFO: URI: api/users/moment 2012-06-13 21:06:29 --- INFO: POST latitude = a------------ThIs_Is_tHe_bouNdaRY_$ Content-Disposition: form-data; name="longetide"

b------------ThIs_Is_tHe_bouNdaRY_$ Content-Disposition: form-data; name="format"

plist------------ThIs_Is_tHe_bouNdaRY_$ Content-Disposition: form-data; name="app_key"

218511044bee5c4e405ec7217b02df8c GET FILES

2012-06-13 21:06:29 --- INFO: **** **** **** KLOG END **** **** **** **** ****

klevison avatar Jun 14 '12 00:06 klevison

For multi-part form data it is required to add separators/boundaries so the output looks correct to me afaics. Are you sure your server is expecting a request with body type multipart/form-data?

Is your API open for public? If so, I can check...

ivasic avatar Jun 21 '12 20:06 ivasic

Yeah, my server is expecting a multipart/form-data. We are using something like this:

http://www.w3schools.com/php/php_file_upload.asp

klevison avatar Jun 21 '12 23:06 klevison

Really can't see what could be wrong (if anything)... What I can suggest is that you try stepping through code on your server endpoint and try figuring out where it fails (e.g. it expects X and iOS app is sending Y....)

ivasic avatar Jun 24 '12 14:06 ivasic

All param has a pattern:

PARAM_VALUE ------------ThIsIs_tHe_bouNdaRY$ Content-Disposition: form-data; name="PARAM_NAME"

but, the first param(latitude) hasn't this pattern.

Maybe it is the problem.

klevison avatar Jul 03 '12 18:07 klevison

RFRequest * r=[[RFRequest requestWithURL:url type:RFRequestMethodPost bodyContentType:RFRequestBodyTypeMultiPartFormData resourcePathComponents:nil, nil]retain];

r.rawBytesBodyContentType = @"multipart/form-data";
[r addParam:[NSString stringWithFormat:@"%@\r\n",nick_name] forKey:@"nickname"];
[r addParam:@"0\r\n" forKey:@"sex"];
[r addParam:[NSString stringWithFormat:@"%@\r\n",sign_lable] forKey:@"signature"];

// [r addParam:@"http://118.186.136.13/headpic/default/32.jpg" forKey:@"syspic"]; [r addData:pic_data withContentType:@"image/png" forKey:@"pic"];

@"multipart/form-data";

As a form, upload a picture and a few key / volue, the results failed, do not know why, ask for help? ?

xiaoyu007 avatar Dec 09 '13 03:12 xiaoyu007