0
http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"}

How can I send a key and data to web? Here "email" is the key and "ali" is the value in URL.

Code:

    NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:@"users/add.json?json="]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString * params =@"{\"email\":\"Ali\"}";

    [request setHTTPMethod:@"POST"];
    // This is how we set header fields
   [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Ali
  • 525
  • 12
  • 24
  • 1
    If you are using POST, you should send your data in the body of the request. You could use setHTTPBody: of the request object to do this. Are you sending xml or JSON? You set the Content-Type to "application/xml", but are trying to send JSON-data? – Håkan Fahlstedt Dec 02 '13 at 08:11
  • check this http://stackoverflow.com/a/4466899/1262634 it has your ansewr. – Tarek Hallak Dec 02 '13 at 08:15
  • yes i have changed to application/json but still not working – Ali Dec 02 '13 at 09:32
  • also checked the link and done the same as in link but still not successful – Ali Dec 02 '13 at 10:19
  • This all depends on what the requirements of your server actually are. Can you give us more details about it, please – Mike Abdullah Jan 10 '14 at 10:55

1 Answers1

1

Simply Try with this :

 NSString * params =@"{\"email\":\"Ali\"}";
 NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:@"users/add.json?json=%@",params]];


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog (@"%@",data);
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • This won't work, since you are not setting the corresponding content type header. If the content type header is not set, a server treats the body of a HTTP/1.1 message as `application/octet-stream`. – CouchDeveloper Dec 02 '13 at 08:52
  • @Ali : Sorry , I didn't read properly . Check my edited Answer – Kumar KL Dec 02 '13 at 10:55
  • @KumarKl tried that too but not working have u checked that link http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"} it send a response but in the response i get name = "The request has been black-holed"; url = "/demos/blow/users/add.json"; – Ali Dec 02 '13 at 11:07
  • Yes, I had Checked ... what i getting is **{response: false};** Without Knowing the structure of add.json. Can't decide – Kumar KL Dec 02 '13 at 11:21