Jump to content

Web API add-file with Objective-C help request


Qata

Recommended Posts

Hello everyone, I've been developing a menubar app for OS X that is attempting to use NSURLConnections to send POST data to a µTorrent server.

When I try with a .torrent file it invariably tells me "Error - torrent file content not supplied in form parameter".

I was hoping someone knowledgeable about the API and/or Objective-C could have a gander at it, I would really appreciate any help at all, this is all that stands in the way of an alpha release.

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&action=add-file", [self getBaseURL], token]]];

NSString * boundary = @"!@!@!@!@!@!@!@!@!";
NSMutableData * body = [NSMutableData alloc];

[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"enctype"];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"application/x-bittorrent"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%@\"\r\n", torrentName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Length: %ld\r\n", [torrentFileContents length]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:torrentFileContents]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Link to comment
Share on other sites

  • 1 month later...

I've done this in other languages. In my case, I placed the content-disposition line immediately after the boundary, followed by the content-type. I did not supply a content-length. Possibly this could be done in the order you have, but the double CRLF after the content-type will make the parser expect the contents and then a boundary. Instead you send the content-disposition, probably not good.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...