Skip to content
New issue

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

Option to allow content type in mulipart parameters #37

Open
kand617 opened this issue Sep 10, 2015 · 1 comment
Open

Option to allow content type in mulipart parameters #37

kand617 opened this issue Sep 10, 2015 · 1 comment

Comments

@kand617
Copy link

kand617 commented Sep 10, 2015

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"];
            }
@Josnic
Copy link

Josnic commented May 21, 2022

Regards.

For multipart form data, I use a simple solution (a better solution is welcome). In the same file, I added:

image

@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:
image

And now, with this configuration, I I was able to use a request with multipart, only adding the header in the request:

image

I hope that can be util for more devs that using this awesome library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants