2

I'm trying to understand how to send an image with http POST and my current client-server protocol design. All the messages from client to server looks like the example below, there is a cmd string with parameter cmd and some more relevant parameters for the command.

For example this is how i send a text message to the server:

- (void)sendMessagesWithText:(NSString *)text fromUser:(NSString *)userId
{
    NSString *url = SERVER_URL;

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"cmd=%@&userid=%@&msgtext=%@", 
                       @"sendmessage", 
                       userId,
                       text] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:body];

    // send to server
    [[NetworkHelper sharedManager] sendRequest:request]; 
}

Now I want to enable the user to send also an image, but how do I send it with my protocol design? should i just append the image to the body after the cmd string?

Eyal
  • 10,777
  • 18
  • 78
  • 130

2 Answers2

6

You need to post with multipart/form-data.

Here is the example from the now defunct ASI: Creating and running requests

Sending a form POST with ASIFormDataRequest

To send POST data in a manner compatible with web page forms, use the included ASIFormDataRequest subclass. Data is posted in ‘application/x-www-form-urlencoded’ format, or ‘multipart/form-data’ format when uploading binary data or files. Data in files is read as needed from disk, so POSTing large files is OK, as long as your web server is setup to handle them.

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

ASIFormDataRequest will autodetect the mime type of files (on iOS 3.0 or later an Mac) added to the POST via setFile:forKey:, and include this in the mime headers sent to the server. If you prefer, you can use the longer form to override this:


Multipart/form-data sucks. Trust me, I've implemented it in C++ a number of years ago. Get someone else to do the dirty work.

But if you still want to go it alone. I found this and this. Good Luck.

Community
  • 1
  • 1
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • ASIHTTPRequest is an awesome library. – Wim Haanstra May 29 '12 at 22:50
  • I prefer not to use a library just for this one method. Can u tell me how to do it myself? – Eyal May 29 '12 at 23:03
  • I've updated my answer. I hope it warns you away from trying. – Jeffery Thomas May 30 '12 at 01:26
  • @Jeffery I don't get it, you say that doing it alone is sucks but it's seams like I just need to add some fields to my http body. Do I miss something? is there some more work to deal with the Multipart/form-data? – Eyal May 30 '12 at 06:49
  • I was writing a general purpose tool that could send any content to any kind of HTTP server, so I had to deal with lots of quirks. Servers were picky and didn't produce good error messages. You might have an easier time of it. – Jeffery Thomas May 30 '12 at 11:58
4

Just append an NSData version of the image to the the POST body.

[body appendData:[[NSString stringWithFormat:@"cmd=%@&userid=%@&msgtext=%@", @"sendmessage&image=", userId, text] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData: UIImagePNGRepresentation((UIImage *) myImage)];

However, it might be easier to look in to ASIHTTPRequest, with that, you can just use:

[request setPostValue:myImageData forKey:@"image"];
max_
  • 24,076
  • 39
  • 122
  • 211
  • For others banging their head against the wall, one should first ask the server guys what is required. In my case server end needed a normal file so [body appendData: UIImagePNGRepresentation((UIImage *) myImage)]; works fine but in some cases multipart request is required. I spent the whole evening and didn't succeed, because I was using the multipart request but needed a normal appednData. duh ! – nr5 Feb 17 '15 at 07:01