POST form data in Objective C

972 Views Asked by At

I have to pass form data in body part. here is postman screen short for better understanding. body part enter image description here

content type

enter image description here

here is my code what I had try.

    NSString *Accesstoken = [NSString stringWithFormat:@"Bearer %@",tokenInfo.access_token];

   //    parameter is String object : "user_id=18&deal_id=218"

   //NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                     @"user_id", @"18487",
                     @"deal_id", @"218",
                     nil];

// NSError *errorr; // NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&errorr];

[request setValue:Accesstoken forHTTPHeaderField:@"Authorization"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// NSData *postData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

// [request setHTTPBody:postData];; // [request setHTTPBody:[parameter dataUsingEncoding:NSUTF8StringEncoding]]; // [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:@{@"user_id":@"18487",@"deal_id":@"218" } options:0 error:nil]];

for above code I am getting http status code : 404 its wrong. and when I try in postman I am getting 401 its perfect I want that. Please help Thanks.

1

There are 1 best solutions below

0
On

Just export the request from POSTMAN in objective-C : https://www.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets