Tutorial

POSTing Form Data From iOS

I’ve had a couple people approach me and ask me how to POST to a web form with a mix of file data and other text values. After much research and lack of resources on the internet describing how to do so, I eventually stumbled upon a library, ASIHTTPRequest which makes it quite easy to do so.

Make sure you get the name attributes of all values you are trying to POST to your web service. In this particular web service, Blotify, we accept an image along with several values pertaining to the particular image. The file attribute is named media.

First, you want to translate your raw image data from the camera into a UIImage, and then into a NSData object. Secondly, set the URL to POST to.

NSData *imageData = UIImageJPEGRepresentation(image, 70);
NSString *urlString = [NSString stringWithFormat:@"http://api.blotify.com/upload/"];

Next, create your ASIFormDataRequest object and add the values you want to post to the server, making sure the keys align with the name attributes on the form of your web service, and send the request by sending the object a startSynchronous message. In this particular web service, we are accepting form values: details, rating, lat, lng, and media for the actual file.

ASIFormDataRequest *asiRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
[asiRequest addPostValue:@"in the lab" forKey:@"details"];
[asiRequest addPostValue:@"3" forKey:@"rating"];
[asiRequest addPostValue:@"35.296574" forKey:@"lat"];
[asiRequest addPostValue:@"-120.668886" forKey:@"lng"];
[asiRequest addData:imageData withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:@"media"];
[asiRequest startSynchronous];

To make debugging easier, you may want to log any messages the server responds with as such:

NSError *error = [asiRequest error];
if (!error) {
NSString *response = [asiRequest responseString];
NSLog(@"upload response: %@", response);
}

Here is a sample form this code coule be POSTing to, this may not be necessary, in the case that you are writing the back end to your own web service.

<form action="http://api.blotify.com/upload/" method="post" enctype="multipart/form-data">
Details: <input type="text" name="details" /><br>
Rating: <input type="text" name="rating" /><br>
Latitude: <input type="text" name="lat" /><br>
Longitude: <input type="text" name="lng" /><br>
<input type="file" name="media" /></br>
<input type="submit" name="submit" value="Upload" />
</form></code>

There you have it, with very minimal amount of code, you can start posting files and values to your favorite web services from iOS using the ASIHTTPRequest library! There is much more you can do with the library, I have just touched the surface with this post. If you would like to read more about the capabilities of this library, check out their wonderful documentation!

(Visited 1,685 times, 1 visits today)
Previous Post Next Post

You Might Also Like

7 Comments

  • Reply Russell July 4, 2014 at 4:27 PM

    Very good info. Lucky me I came across your site by accident (stumbleupon).
    I’ve bookmarked it for later!

    my web-site: targeted search terms (Russell)

  • Reply Startup Coworking Space San Francisco July 4, 2014 at 8:32 PM

    This paragraph gives clear idea designed for the
    new people of blogging, that genuinely how to
    do blogging.

    Feel free to visit my weblog :: Startup Coworking Space San Francisco

  • Reply Best Coworking Spaces in San Francisco July 25, 2014 at 3:48 PM

    Hey there I am so thrilled I found your weblog, I really found you by error,
    while I was browsing on Askjeeve for something else, Anyways I am here now and would just
    like to say thanks a lot for a marvelous post and a all
    round exciting blog (I also love the theme/design), I don’t have time to read through it
    all at the moment but I have bookmarked it and also
    added in your RSS feeds, so when I have time I will be back to read a lot more,
    Please do keep up the excellent jo.

    Feel free to surf to my webpage – Best Coworking Spaces in San Francisco

    • Reply Minerva January 27, 2016 at 6:53 AM

      I read your posntig and was jealous

    • Reply http://www.rab-jeric.com/ April 30, 2016 at 5:24 PM

      Brett, I haven’t heard that one in a little…Well, if the AI uses nanotechnology to create a gray goo, the technical term for this exact sort of thing, we’ll still have a very long time to wait since even with exponential spread, it would still take decades for this goo to pose any threat, and depending on what this goo is made of, we could have bacteria digest it for us. Come to think of it, this gray goo discussion may warrant a post in its own right…

    • Reply http://www.dropofseaphotography.com/ May 17, 2016 at 1:28 AM

      you just hang in there, girl. we are here even if we are not as close by as we would like to be. teary is okay, too. it washes away the cobwebs so that you can think clearly and see forward to the next step. who is normal anyway?

    • Reply http://www.passstylemusic.com/ July 15, 2016 at 2:27 AM

      The cat-ownership/schizophrenia connection has been known for over a decade. Small but persistent. Childhood exposure to cats seems to be a bigger issue than adult contact, though that one's more uncertain.

    Leave a Reply