iosobjective-cpostnsdatagcdwebserver

How to read form body of a GCDWebServer POST request


I'm struggling to believe this is so difficult but after spending a couple of hours trying to figure it out, here I am asking the hive mind for help!

My question is very similar to this one however the answers in there don't help me as I have no keys in my form. I basically want to send a text .plist file from a PC to my app. The HTML on the page is this ..

<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" id="config" name="config" accept=".plist" size=40>
<input type="submit"> 
</form>

This gives a very simple form with a chose file button and a submit button. I have confirmed with Wireshark that the file is attached to the form and the thing is POSTed to the GCDWebServer in my app. I can also see the following confirming the body data as the file upload is processed by the web server:

[DEBUG] Connection on socket 21 preflighting request "POST /upload" with 2045 bytes body

[DEBUG] Connection on socket 21 processing request "POST /upload" with 2045 bytes body

My POST method for handling the file is this:

    [_webServer addHandlerForMethod:@"POST"
                          path:@"/upload"
                  requestClass:[GCDWebServerRequest class]
                   processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
    
        NSString    *type = [(GCDWebServerRequest*)request contentType];
        NSInteger  length = [(GCDWebServerRequest*)request contentLength];
        BOOL      hasBody = [(GCDWebServerRequest*)request hasBody];
    
    NSString *description = [(GCDWebServerURLEncodedFormRequest*)request description];
    
    NSLog(@"\r\nType: %@\nLength: %lu\nHas Body: %@\nHeaders: %@\nConfig: %@", type, length, hasBody?@"YES":@"NO", description, @"");
    
        return [GCDWebServerDataResponse responseWithHTML:@"TODO UPLOADED CONFIRMATION"];
    }];

I can get the content type, content length, a boolean confirmation there is a body attached, all the headers and description of the boundaries etc. But I cannot for the life of me figure out how to get the actual body content. I don't wish to save it as a file I simply want it as an NSString so I can parse it and then save the settings inside my app.

A few things I have tried:

//NSData     *data = [(GCDWebServerRequest*)request data];
//NSString *config = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

//NSString *config = [(GCDWebServerMultiPartFormRequest*)request files];
//NSString *config = [[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"filename"];

The NSData one looks the most likely to me but the app crashes when calling that. If anyone has any idea how I might access the body of this posted content I would be extremely grateful!

Thanks in advance!

Plasma


Solution

  • You'll want to use GCDWebServerMultiPartFormRequest. There's an an example here in https://github.com/swisspol/GCDWebServer/blob/master/Mac/main.m#L257.