Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created October 19, 2011 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CastIrony/1298546 to your computer and use it in GitHub Desktop.
Save CastIrony/1298546 to your computer and use it in GitHub Desktop.
-(void)submitPostRequestWithUrl:(NSURL*)url
{
NSString* boundary = @"---------------------------14737809831466499882746641449";
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:self.timeOut];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSData* postbody = [NSData dataForPOSTWithDictionary:self.postParameters boundary:boundary];
[request setHTTPBody:postbody];
self.request = request;
[self submitRequest];
}
+(NSData*)dataForPOSTWithDictionary:(NSDictionary *)dictionary boundary:(NSString *)boundary
{
NSMutableData* postBodyData = [NSMutableData data];
for(NSString* key in dictionary)
{
id value = [dictionary valueForKey:key];
if([value isKindOfClass:[NSData class]])
{
[postBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", key, key] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:value];
}
else
{
[postBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBodyData appendData:[[NSString stringWithFormat:@"%@", value] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBodyData appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
return postBodyData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment