Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (43 sloc) 2.359 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//
// IRWebAPIEngine+FormURLEncoding.m
// IRWebAPIKit
//
// Created by Evadne Wu on 11/7/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "IRWebAPIHelpers.h"
#import "IRWebAPIEngine+FormURLEncoding.h"

NSString * const kIRWebAPIEngineRequestContextFormURLEncodingFieldsKey = @"IRWebAPIEngineRequestContextFormURLEncodingFields";

@implementation IRWebAPIEngine (FormURLEncoding)

+ (IRWebAPIRequestContextTransformer) defaultFormURLEncodingTransformer {

return [[(^ (NSDictionary *inOriginalContext) {

NSDictionary *formNamesToContents = [inOriginalContext objectForKey:kIRWebAPIEngineRequestContextFormURLEncodingFieldsKey];

if (![formNamesToContents count])
return inOriginalContext;

NSMutableDictionary *returnedContext = [[inOriginalContext mutableCopy] autorelease];
NSMutableDictionary *headerFields = [returnedContext objectForKey:kIRWebAPIEngineRequestHTTPHeaderFields];

if (!headerFields) {
headerFields = [NSMutableDictionary dictionary];
[returnedContext setObject:headerFields forKey:kIRWebAPIEngineRequestHTTPHeaderFields];
}

[headerFields setObject:@"8bit" forKey:@"Content-Transfer-Encoding"];
[headerFields setObject:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];

NSData *sentData = IRWebAPIEngineFormURLEncodedDataWithDictionary(formNamesToContents);

[returnedContext setObject:sentData forKey:kIRWebAPIEngineRequestHTTPBody];

[returnedContext removeObjectForKey:kIRWebAPIEngineRequestContextFormURLEncodingFieldsKey];

[returnedContext setObject:@"POST" forKey:kIRWebAPIEngineRequestHTTPMethod];

return (NSDictionary *)returnedContext;

}) copy] autorelease];

}

@end


NSData * IRWebAPIEngineFormURLEncodedDataWithDictionary (NSDictionary *formNamesToContents) {

NSMutableData *sentData = [NSMutableData data];

[formNamesToContents enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL *stop) {

if ([sentData length])
[sentData appendData:[@"&" dataUsingEncoding:NSUTF8StringEncoding]];

[sentData appendData:[IRWebAPIKitRFC3986EncodedStringMake(key) dataUsingEncoding:NSUTF8StringEncoding]];
[sentData appendData:[@"=" dataUsingEncoding:NSUTF8StringEncoding]];
[sentData appendData:[IRWebAPIKitRFC3986EncodedStringMake(obj) dataUsingEncoding:NSUTF8StringEncoding]];

}];

return sentData;

}
Something went wrong with that request. Please try again.