0

I am passing HTTPBody in my request. But its unable to get proper response.

below is my code :

NSURL *url = [NSURL URLWithString:@"https://sample.ios.com/l1/voucher"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

//Now pass your own parameter --------

[request setValue:@"g064a4b5aa95556a7b0b4435d94c317840e9b456" forHTTPHeaderField:@"X-Authorization"];
[request setValue:@"123651236512" forHTTPHeaderField:@"uuid"];
[request setValue:@"myIphone" forHTTPHeaderField:@"devicename"];


NSString *myRequestString =@"voucher=JHAHSDGH-80";
NSLog(@"%@",myRequestString);
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
[ request setHTTPBody: myRequestData ];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"content : %@",content);

Output is coming like below :

content : {"voucher":["The voucher field is required."]}

So how can I set HTTPBody ?

Please help me.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51

1 Answers1

2

HTTPBody is set to be as NSData, Which is the type of NSJSONSerialization class.

So your solution would be, make an NSArray and embedded into NSDictionary class as your web services required.

Then use [NSJSONSerialization dataWithJSONObject:'Your dictionary' options:NSJSONWritingPrettyPrinted error:&error] To convert a JSON into Data And it will return a NSData class obj.

Then you add to your request as, [request setHTTPBody: Data]

Hope this will help you. Let me know if I got your question wrongly.

  • @SRNayak : What is your HTTPBody need to feed? – Deepak Kumar Sahu Nov 15 '16 at 08:02
  • NSArray * name = @[a, b, c] NSDictionary * dictionary = [[NSDictionary alloc]initWithObjects:@[name,@“hello",] forKeys:@[@"firstName",@"message"]]; NSData *data=[NSJSONSerialization dataWithJSONObject: dictionary options:NSJSONWritingPrettyPrinted error:nil] [requestToPostDataField setHTTPBody:data]; Try this method. – Deepak Kumar Sahu Nov 15 '16 at 08:03
  • My body will be voucher= GSJWBJEB-90 – Soumya Ranjan Nov 15 '16 at 08:07
  • NSDictionary * dictionary = [[NSDictionary alloc]initWithObject:@[] forKeys:@[@"firstName",@"message"]]; NSData *data=[NSJSONSerialization dataWithJSONObject: dictionary options:NSJSONWritingPrettyPrinted error:nil] [requestToPostDataField setHTTPBody:data] – Deepak Kumar Sahu Nov 15 '16 at 08:35
  • NSDictionary * dictionary = [[NSDictionary alloc]initWithObject:@[@"voucher"] forKey:@[@"GSJWBJEB-9"]]; NSData *data=[NSJSONSerialization dataWithJSONObject: dictionary options:NSJSONWritingPrettyPrinted error:nil] [requestToPostDataField setHTTPBody:data] Try this one – Deepak Kumar Sahu Nov 15 '16 at 08:37
  • same issue "content : {"voucher":["The voucher field is required."]}" is coming – Soumya Ranjan Nov 15 '16 at 09:16
  • NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"GSJWBJEB-90",@"voucher", nil]; NSData * data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]); – Deepak Kumar Sahu Nov 15 '16 at 11:23
  • Output: { voucher = "GSJWBJEB-90"; } – Deepak Kumar Sahu Nov 15 '16 at 11:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128155/discussion-between-s-r-nayak-and-deepak-kumar-sahu). – Soumya Ranjan Nov 15 '16 at 11:29