Table of contents

POST request using application/x-www-form-urlencoded with Objective C

Objective C Mar 28, 2020 Viewed 57 Comments 0

Using Objective C to post request withapplication/x-www-form-urlencodedbody. The data will be encoded as key-value pairs separated by '&'. This type does not support binary data. If you want to send a file, refer to Example of multipart/form-data post in Objective C.



#import <Foundation/Foundation.h>

@interface HttpPostForm:NSObject
-(id)initWithUrl:(NSString *)urlString;

@property(nonatomic) NSMutableURLRequest *request;
@property(nonatomic) NSString *urlString;
@property(nonatomic) NSMutableDictionary *headers;
@property(nonatomic) NSMutableDictionary *fields;

/// Adds a header to the request
/// @param key Header key
/// @param value Header value
-(void)addHeader:(NSString *)key value:(NSString *)value;

/// Adds a form field to the request
/// @param key Field name
/// @param value Field value
-(void)addFormField:(NSString *)key value:(NSString *)value;

/// Completes the request
/// @param callback The callback block
- (void)finish:(void(^)(NSData *, NSURLResponse *, NSError *))callback;



#import "HttpPostForm.h"

@implementation HttpPostForm

-(id)initWithUrl:(NSString *)urlString{
  self.request = [[NSMutableURLRequest alloc]init];
  self.urlString = urlString;
  self.headers = [[NSMutableDictionary alloc]init];
  self.fields = [[NSMutableDictionary alloc]init];
  return self;

-(void)addHeader:(NSString *)key value:(NSString *)value{
  [self.headers setObject:value forKey:key];

-(void)addFormField:(NSString *)key value:(NSString *)value{
  [self.fields setObject:value forKey:key];

- (void)finish:(void (^)(NSData *, NSURLResponse *, NSError *))callback{
  NSURL *url = [[NSURL alloc]initWithString:self.urlString];
  [self.request setURL:url];
  [self.request setHTTPMethod:@"POST"];
  // Add haders
  for (NSString *key in self.headers){
    NSString *value = self.headers[key];
    [self.request setValue:value forHTTPHeaderField:key];
  // Add Content-Type
  [self.request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  // Add fields
  NSMutableData *body = [NSMutableData data];
  for (NSString *key in self.fields){
    NSString *value = self.fields[key];
    [body appendData:[[NSString stringWithFormat:@"%@=%@", key, value] dataUsingEncoding:NSUTF8StringEncoding]];
  if ([body length] > 0) {
    [self.request setHTTPBody:body];
    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%ld", [body length]];
    [self.request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  void (^completionHandler)(NSData *, NSURLResponse *, NSError *) = ^(NSData *data, NSURLResponse *response, NSError *error) {
    if (callback) {
      callback(data, response, error);
  [[[NSURLSession sharedSession] dataTaskWithRequest:self.request completionHandler:completionHandler] resume];


Use it

NSString *url = @"http://localhost";
HttpPostForm *request = [[HttpPostForm alloc]initWithUrl:url];
// Add header user-agent
[request addHeader:@"User-Agent" value:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"];
// Add Form field
[request addFormField:@"username" value:@"test"];

void (^completionHandler)(NSData *, NSURLResponse *, NSError *) = ^(NSData *data, NSURLResponse *response, NSError *error) {
    if (data == nil || data.length == 0 || response == nil || [(NSHTTPURLResponse *)response statusCode] != 200) {
    NSLog(@"success: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
[request finish:completionHandler];
Updated Mar 28, 2020