Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Post file and extra parameters #4

Open
olkarls opened this issue May 23, 2012 · 7 comments
Open

Post file and extra parameters #4

olkarls opened this issue May 23, 2012 · 7 comments

Comments

@olkarls
Copy link

olkarls commented May 23, 2012

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

@ivasic
Copy link
Owner

ivasic commented May 23, 2012

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

@klevison
Copy link

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 **** **** **** **** ****

@ivasic
Copy link
Owner

ivasic commented Jun 21, 2012

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...

@klevison
Copy link

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

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

@ivasic
Copy link
Owner

ivasic commented Jun 24, 2012

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....)

@klevison
Copy link

klevison commented Jul 3, 2012

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.

@xiaoyu007
Copy link

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? ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants