0
NSString *loginurl =[NSString
                     stringWithFormat:@"/ajax/useraccount.aspx/methodname"]
NSURL *url = [NSURL URLWithString:loginPath];

NSString *loginPath = [NSString stringWithFormat:@"%@%@",singleton.website,loginurl];

asiRequest = [[ASIHTTPRequest requestWithURL:url] retain];
[asiRequest addRequestHeader:@"Content-Type" value:@"application/json"];
[asiRequest addRequestHeader:@"Accept" value:@"application/json"];
[asiRequest setRequestMethod:@"POST"];

NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                     @"email", @"Email",
                     @"fname", @"FirstName", nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
NSLog(@"this is value %@",postdata);
NSMutableData *mutablePostData = [NSMutableData dataWithData:postdata];


[asiRequest setPostBody:mutablePostData];
[asiRequest setDelegate:self];
[asiRequest setTimeOutSeconds:60];
[asiRequest startAsynchronous];
[activityIndicator startAnimating];

i want create this type of object and set it in a post body {"objectRequest":{"email":"Email","fname":"FirstName"}}

i am unable to hit the method am i missing something or is there is another way to create this type of json object and set it in a post body thank you in advance

  • You don't need the "" around the key on the object. You should just use {objectRequest:{email:"Email",fname:"FirstName"}} – Pablo Jomer Aug 15 '14 at 07:31
  • i have tried this as well but not working i:e NSString *jsonRequest = [NSString stringWithFormat:@"{\registartionrequest:{\"Email\":\"%@\",\"FirstName\":\"%@\"}}",@"test",@"test"]; NSLog(@"Request: %@", jsonRequest); NSData *postData =[[NSStringstringWithFormat:@"%@",jsonRequest] dataUsingEncoding:NSUTF8StringEncoding]; – malik rizwan Aug 15 '14 at 07:47

2 Answers2

0

You can really just return that string as a body if you like. But you have to set the content type to "application/json". One of these posts might help for doing that on ios?

How to set content-type when using initWithContentsOfUrl

JSON file set content type to application/json AFNetworking

setting content-type overwriting httpbody?

Community
  • 1
  • 1
Pablo Jomer
  • 7,990
  • 6
  • 45
  • 86
  • i am already setting its content-type to application/json [asiRequest addRequestHeader:@"Content-Type" value:@"application/json"]; [asiRequest addRequestHeader:@"Accept" value:@"application/json"]; – malik rizwan Aug 15 '14 at 07:35
0

Everything looks ok with your code except you forgot to call buildPostBody method. After you set all body data you have to call this method.

[asiRequest buildPostBody];
[asiRequest startAsynchronous];

If this is a new project, I highly recommend to use any other up-to-date libraries. developers stopped developing ASIHTTPRequest long time ago.

mohacs
  • 12,002
  • 9
  • 47
  • 76