We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi,
It would be great if Unirest can allow sending of content types each part in the multipart formdata In the file : UNIHTTPClientHelper.m
if ([requestWithBody body] == nil) { // Has parameters NSDictionary* parameters = [requestWithBody parameters]; bool isBinary = [UNIHTTPClientHelper hasBinaryParameters:parameters]; if (isBinary) { [headers setObject:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY] forKey:@"content-type"]; for(id key in parameters) { id value = [parameters objectForKey:key]; if ([value isKindOfClass:[NSURL class]] && value != nil) { // Don't encode files and null values [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; NSString* filename = [[value absoluteString] lastPathComponent]; NSData* data = [NSData dataWithContentsOfURL:value]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, filename] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Length: %d\r\n\r\n", data.length] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:data]; } else { if(value is of type nsdict){ //Check here if value is a dictionary.... the keys could be : "content-type", "value" }else{ [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@", value] dataUsingEncoding:NSUTF8StringEncoding]]; } } } // Close [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; } else { NSString* querystring = [UNIHTTPClientHelper dictionaryToQuerystring:parameters]; body = [NSMutableData dataWithData:[querystring dataUsingEncoding:NSUTF8StringEncoding]]; [headers setValue:@"application/x-www-form-urlencoded" forKey:@"content-type"]; }
The text was updated successfully, but these errors were encountered:
Regards.
For multipart form data, I use a simple solution (a better solution is welcome). In the same file, I added:
@interface UNIHTTPClientHelper() + (NSString*) encodeURI:(NSString*)value; + (NSString*) dictionaryToQuerystring:(NSDictionary*) parameters; + (BOOL) hasBinaryParameters:(NSDictionary*) parameters; + (BOOL) hasMultipartFormDataHeader:(NSDictionary*) headers; + (NSMutableURLRequest*) prepareRequest:(UNIHTTPRequest*) request; + (UNIHTTPResponse*) getResponse:(NSURLResponse*) response data:(NSData*) data; @end @implementation UNIHTTPClientHelper + (BOOL) hasBinaryParameters:(NSDictionary*) parameters { for(id key in parameters) { id value = [parameters objectForKey:key]; if ([value isKindOfClass:[NSURL class]]) { return true; } } return false; } + (BOOL) hasMultipartFormDataHeader:(NSDictionary*) headers { for(id key in headers) { id value = [headers objectForKey:key]; if ([[value lowercaseString] compare:@"multipart/form-data" options:NSCaseInsensitiveSearch] == NSOrderedSame && [[key lowercaseString] compare:@"content-type" options:NSCaseInsensitiveSearch] == NSOrderedSame ){ return true; } } return false; }
This check the request headers searching a content-type with multipart/form-data. After, I implemented here:
And now, with this configuration, I I was able to use a request with multipart, only adding the header in the request:
I hope that can be util for more devs that using this awesome library.
Sorry, something went wrong.
No branches or pull requests
Hi,
It would be great if Unirest can allow sending of content types each part in the multipart formdata
In the file : UNIHTTPClientHelper.m
The text was updated successfully, but these errors were encountered: