Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are not appending the terminating boundary.</p> <p>This:</p> <pre><code>[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; </code></pre> <p>Should look like this:</p> <pre><code>[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; </code></pre> <p><strong>EDIT:</strong></p> <p>You are missing some more things actually. You are not setting the content type for the data and you're missing some new lines.</p> <p>Your loop should look like this:</p> <pre><code>for (NSString *param in params) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Type: text/plain\r\n"] dataUsingEncoding:NSUTF8StringEncoding]] [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } [body appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; </code></pre> <p><strong>EDIT2:</strong></p> <p>Here's an example server-side code which is handling that same request correctly:</p> <pre><code>#!/usr/bin/env python import web urls = ("/", "Index") app = web.application(urls, globals()) class Index(object): def POST(self): x = web.input() print x print x['apikey'] print x['scope'] return 'success' if __name__ == "__main__": app.run() </code></pre> <p>Output:</p> <pre><code>&lt;Storage {'scope': u'asd', 'apikey': u'123\r\n'}&gt; 123 asd 127.0.0.1:60044 - - [02/Dec/2013 15:02:55] "HTTP/1.1 POST /" - 200 OK </code></pre> <p>And sample console app. which you can compile with clang - <code>clang -framework Foundation test_post.m -o test_post</code></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Uploader : NSObject &lt;NSURLConnectionDataDelegate&gt; @property (nonatomic, strong) NSDictionary *parameters; @property (nonatomic, strong) NSURLConnection *connection; @end @implementation Uploader - (id)init { if (self = [super init]) { } return self; } - (NSMutableURLRequest*)createRequest { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://localhost:8080/"]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"0xB0un9ArY"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; [request setValue:@"1.0" forHTTPHeaderField:@"MIME-Version"]; [request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"]; NSMutableData *body = [NSMutableData data]; for (NSString *key in [self.parameters allKeys]) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Type: text/plain\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", (self.parameters)[key]] dataUsingEncoding:NSUTF8StringEncoding]]; } [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; return request; } - (void)upload { NSMutableURLRequest *request = [self createRequest]; self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (self.connection) [self.connection start]; } - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {NSLog(@"connection:didReceiveResponse: %@", response);} - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {NSLog(@"connection: didReceiveData: %@", data);} - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {NSLog(@"connection: didFailWithError: %@", error);} - (void)connectionDidFinishLoading:(NSURLConnection*)connection { NSLog(@"connectionDidFinishLoading:"); } @end int main(int argc, char *argv[]) { @autoreleasepool { NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; Uploader *uploader = [[Uploader alloc] init]; uploader.parameters = @{@"apikey": @"123", @"scope": @"asd"}; [uploader upload]; [runLoop run]; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload