ASIFormDataRequest returns NULL when trying to request a POST method

518 views Asked by At

Hi im trying to make a POST request

my code:

NSURL *url = [NSURL URLWithString:urlString];
    __weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setRequestMethod:@"POST"];
    [request setPostValue:@"JustinBieber" forKey:@"fname"];

    [request setCompletionBlock:^{
        NSString *result = [request responseString];
        NSDictionary *dict = [result JSON];
        NSLog(@"dict -%@",dict);
    }];
    [request setFailedBlock:^{
        NSLog(@"error %@",[request error]);

    }];
    [request startAsynchronous];

when I run my code it returns a (null) value. My urlString is correct and the request didn't give me error also. I've tried it on web and returns a {"status":"success"} (it will return a dictionary with status successful or failed).

4

There are 4 answers

5
Vibha Singh On

Use Like this. Hope this will help.

-(void)exe method
 {
     NSString *strURL=@"---your URL----";
     NSURL *url=[NSURL URLWithString:strURL];
     ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
     [request setRequestMethod:@"POST"];
     [request setPostValue:@"JustinBieber" forKey:@"fname"];
     [request setDelegate:self];
     [request setTimeOutSeconds:60];
     [request startAsynchronous];
}


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

    NSError *error;
    if(!error)
    {

        NSString *receivedString = [request responseString];
        NSDictionary *dic = [receivedString JSONValue];
       NSLog(@"output %@",dic);

     }


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


}
0
Sander Saelmans On

Did you set your headers in the script that returns JSON correctly? Assuming you're using PHP:

header('Content-Type: application/json');

The default MIME-type is "text/plain", instead of "application/json". If you dont set the MIME-type correctly you're basicly trying to parse the whole document.



So instead of:

{"status":"success"}

You are most likely trying to parse:

<html>
  <head></head>
  <body>{"status":"success"}</body>
</html>
4
Santosh Sharma On

Try with this code -

NSURL *url = [NSURL URLWithString:urlString];
__unsafe_unretained ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:@"JustinBieber" forKey:@"fname"];
[request setDelegate:self];

__block id jsonData;
[request setTimeOutSeconds:300];
[request setCompletionBlock:^(){
    NSError *error = nil;
    NSString *responseString = (request.responseString.length)?request.responseString:@"";
    NSLog(@"%@",responseString);
    NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    if(error)
        completionBlock(nil, error, task);
    else
        completionBlock(jsonData, error, task);
}];

[request setFailedBlock:^{
    completionBlock(nil, request.error, task);
}];
[request startAsynchronous];

Might be it will helpfull for you.

0
Franco On
-(void)exe method
 {
     NSString *strURL=@"---your URL----";
     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
     [request setDelegate:self];
     [request setRequestMethod:@"POST"];
     [request setPostValue:@"JustinBieber" forKey:@"fname"];
     [request setTimeOutSeconds:60];
     [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
        NSString *receivedString = [request responseString];
       NSLog(@"output %@",receivedString );

}
- (void)requestFailed:(ASIHTTPRequest *)request 
{
       NSString *receivedString = [request responseString];
       NSLog(@"output %@",receivedString );

}