Search code examples
iosjsonpostclient-serverasihttprequest

Create iOS client server app. ASIFormDataRequest issues


My question is: How can I create the same request as below using ASIFormDataRequestand second as I think I need to parse JSON from response.

http://exampledomain.com/mobile_api/register/?client=iphone&info=A=iPhone/OS=5.1/C=UA&time=1342780143&udid=8b6f0cc104d137ae2e1730235f5664094b831122&version=1.0&secure=5444d72741bad93b916577d9297fa

Now I try to use code like this

NSString *strURL = @"http://test2.mafia.ua/mobile_api/register/";
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
    [request setDelegate:self];
    [request setPostValue:@"iphone" forKey:@"client"];
    [request setPostValue:@"A=iPhone/OS=5.1/C=UA" forKey:@"info"];
    [request setPostValue:@"1342780143" forKey:@"time"];
    [request setPostValue:@"8b6f0cc104d137ae2e1730235f5664094b831122" forKey:@"udid"];
    [request setPostValue:@"1.0" forKey:@"version"];
    [request setPostValue:@"5444d72741bad93b916577d9297fa" forKey:@"secure"];
    [request startAsynchronous];

is this correct?

I implement delegate method also

- (void)requestFinished:(ASIHTTPRequest *)request {

    NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]);

}
- (void)requestFailed:(ASIHTTPRequest *)request {

}

In response I get this (I think this is JSON) if I use some browser.

{"status": "ok", "secret": "b82b7771f600772c2c5af903b117b5e", "client": "200004", "expires": "1345372850"}


Solution

  • You're passing your data in POST fields (That's what setPostValue: does.)

    You should be including them in your URL instead; try searching around for an easy way of building query parameters in a URL or just use something like stringWithFormat: and appropriate escaping.