OSX COCOA HTTP POST method

For those of you interested in COCOA Objective-C development, here is some code to proper achieve an upload using HTTP POST.
The input data as you can see is provided by some NSTextFields.
The HTTP request is done Synchronously but there is a simple way to do it asynchronously.
So here you go :

————————————————————————-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
- (IBAction)upload:(id)sender
{
 
    //creating the url request:
    NSURL *cgiUrl = [NSURL URLWithString:@"http://www.testsite.com/upload.php"];
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
 
    //adding header information:
    [postRequest setHTTPMethod:@"POST"];
 
    NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
 
 
    //setting up the body:
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:[edTitle stringValue]] dataUsingEncoding:NSUTF8StringEncoding]];
 
 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:[edDescript stringValue]] dataUsingEncoding:NSUTF8StringEncoding]];
 
 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:[edUser stringValue]] dataUsingEncoding:NSUTF8StringEncoding]];
 
 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:[edPasswd stringValue]] dataUsingEncoding:NSUTF8StringEncoding]];
 
 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"videoFile\"; filename=\"@\"\r\n", [[edFileName stringValue] lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary/video\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 
    [postBody appendData:[NSData dataWithContentsOfFile:[edFileName stringValue]]];
 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postRequest setHTTPBody:postBody];
 
 
    NSError *error;
    NSData *searchData;
    NSHTTPURLResponse *response;
    int n = 0;
 
    //==== Synchronous call to upload
    searchData = [ NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
 
    while (1)
    {
        if ([ response allHeaderFields])
        {
            NSString *monster = [[ response allHeaderFields] valueForKey:@"Monstermsg"] ;
            NSAlert *alert = [[NSAlert alloc] init];
            [alert addButtonWithTitle:@"OK"];
            [alert setMessageText: monster ];
            [alert setAlertStyle:NSWarningAlertStyle];
            if ([alert runModal] == NSAlertFirstButtonReturn)
            {
                NSLog(@"%@", monster);
            }
            [alert release];
            break;
        }
        NSLog(@"%d", n++);
    }
}
  • Share/Bookmark

Tags: , , , , , ,

Friday, April 11th, 2008 COCOA

3 Comments to OSX COCOA HTTP POST method

  • ZevEisenberg says:

    This is exactly what I’ve been looking for, but I’m developing for iPhone and my skill level in Cocoa and Objective C is beginner (although I’ve had experience with other languages). What would I have to do to modify this for iPhone, besides changing the alert to a UIAlert? Thanks.

  • ZevEisenberg says:

    Actually, I got it working, but the errors/responses come back as null. How do I set up the php on the other end to send back meaningful responses that can be parsed by NSURLConnection?

  • sonix says:

    Unfortunately it’s been a while since I’ve programmed Cocoa and it seems I’ve lost my touch :) . In the past year I’ve been dealing with some Delphi, PHP, JS and FLEX but I have no idea on iPhone development. Sorry I cannot help.
    Soon though I’ll get back to OSX programming as I have to port a media streaming application I’ve made for Windows platform and then I’ll post here some interesting ideas and code samples.

    Regards…

  • Leave a Reply

    You must be logged in to post a comment.