nst / STHTTPRequest

Obj-C / Cocoa HTTP requests for humans
BSD 3-Clause "New" or "Revised" License
826 stars 75 forks source link

multipartContentWithBoundary needs a "CR LF" before the boundary #42

Closed sburlot closed 8 years ago

sburlot commented 8 years ago

When sending multiples files with addDataToUpload:parameterName:mimeType:fileName, only the first file is recognized by the server. The boundary is sent after each file, but it needs a "\r\n" before, as specified in the HTML Spec. In STHTTPRequest.m: in + (NSData *)multipartContentWithBoundary:(NSString *)boundary data:(NSData *)someData fileName:(NSString *)fileName parameterName:(NSString *)parameterName mimeType:(NSString *)aMimeType

[data appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

should be

[data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

EDIT: Microsoft IIS seems to be picky: if the data starts with CR LR, it throws an error 500. Nginx/PHP doesnt care.

Adding the CRLF after the data works OK

So correction is to add this line [data appendData:[@"\r\n"dataUsingEncoding:NSUTF8StringEncoding]]; Before returning the data from multipartContentWithBoundary