Skip to content

Ampersands and generatePOSTBodyFromDictionary #2

Description

@prendio2

I encountered an error when fetching data for artists with ampersands in their name, like "Simon & Garfunkel"

The ampersand needs to be url endcoded or the artist named will be concatenated, in this case returning details for "Simon".

I added the following function, originally found at http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html

- (NSString *)urlEncodeValue:(NSString *)str
{
    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
    return [result autorelease];
}

and adjusted generatePOSTBodyFromDictionary as follows

[rawBody appendString:[NSString stringWithFormat:@"&%@=%@", key, [self urlEncodeValue:[dict objectForKey:key]]]];

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions