Permalink
Browse files

support POSTing a dictionary of key/value pairs

  • Loading branch information...
samsonjs committed Dec 19, 2011
1 parent 25cfde7 commit d113809f7cb72a5bbfb0434c43056b61296c9519
Showing with 20 additions and 1 deletion.
  1. +2 −0 Marshmallows/MMHTTPClient.h
  2. +18 −1 Marshmallows/MMHTTPClient.m
@@ -28,6 +28,7 @@
+ (MMHTTPRequest *) getImage: (NSString *)url then: (MMHTTPImageCallback)callback;
+ (MMHTTPRequest *) getText: (NSString *)url then: (MMHTTPTextCallback)callback;
+ (MMHTTPRequest *) post: (NSString *)url then: (MMHTTPCallback)callback;
++ (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback;
+ (MMHTTPRequest *) post: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback;
+ (MMHTTPRequest *) put: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback;
+ (MMHTTPRequest *) delete: (NSString *)url then: (MMHTTPCallback)callback;
@@ -44,6 +45,7 @@
- (MMHTTPRequest *) get: (NSString *)url then: (MMHTTPCallback)callback;
- (MMHTTPRequest *) getImage: (NSString *)url then: (MMHTTPImageCallback)callback;
- (MMHTTPRequest *) getText: (NSString *)url then: (MMHTTPTextCallback)callback;
+- (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback;
- (MMHTTPRequest *) post: (NSString *)url then: (MMHTTPCallback)callback;
- (MMHTTPRequest *) post: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback;
- (MMHTTPRequest *) put: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback;
@@ -7,7 +7,7 @@
//
#import "MMHTTPClient.h"
-#import "NSString+sanity.h"
+#import "NSString+marshmallows.h"
MMHTTPClient *_client;
@@ -98,6 +98,11 @@ + (MMHTTPRequest *) post: (NSString *)url then: (MMHTTPCallback)callback
return [[self sharedClient] post: url then: callback];
}
++ (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback
+{
+ return [[self sharedClient] post: url fields: fields then: callback];
+}
+
+ (MMHTTPRequest *) post: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback
{
return [[self sharedClient] post: url data: data then: callback];
@@ -188,6 +193,18 @@ - (MMHTTPRequest *) post: (NSString *)url then: (MMHTTPCallback)callback
return [self request: options then: callback];
}
+- (MMHTTPRequest *) post: (NSString *)url fields: (NSDictionary *)fields then: (MMHTTPCallback)callback
+{
+ NSMutableArray *parts = [NSMutableArray array];
+ NSString *value;
+ for (NSString *key in [fields keyEnumerator]) {
+ value = [fields objectForKey: key];
+ [parts addObject: [NSString stringWithFormat: @"%@=%@", [key stringByURLEncoding], [value stringByURLEncoding]]];
+ }
+ NSString *body = [parts componentsJoinedByString: @"&"];
+ return [self post: url data: [body dataUsingEncoding: NSUTF8StringEncoding] then: callback];
+}
+
- (MMHTTPRequest *) post: (NSString *)url data: (NSData *)data then: (MMHTTPCallback)callback
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

0 comments on commit d113809

Please sign in to comment.