0

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).

Bordz
  • 2,810
  • 4
  • 23
  • 27

4 Answers4

0

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 
{


}
Vibha Singh
  • 623
  • 4
  • 9
  • thanks for the reply, but still return (null) though. – Bordz Sep 08 '14 at 12:13
  • Check for URL and Key with its value..OR show me URL so that I can check at my end. – Vibha Singh Sep 08 '14 at 12:15
  • My URL is absolutely working I've even tried to do it on web and it returns a status success. – Bordz Sep 08 '14 at 12:19
  • Yes but may be in URL which you are using as an String is GET or POST. for ex: www.xyz.com/files/account?fname=%@ if your URL like this then string value should be NSString *strURL=[NSString stringwithformat:@"www.xyz.com/files/account?fname=%@",value]; and don't post like [request setPostValue:@"JustinBieber" forKey:@"fname"]; – Vibha Singh Sep 08 '14 at 12:25
  • I got what you mean, thanks, my URL is on POST, http://website.com/appName/report/public/index.php/insert – Bordz Sep 08 '14 at 12:52
0

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>
Sander Saelmans
  • 831
  • 4
  • 13
0

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.

Santosh Sharma
  • 421
  • 3
  • 14
  • Hi it gives me an error I think its the handling of the reply which is dictionary. ERROR:error 1 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8f5ab50 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – Bordz Sep 08 '14 at 12:56
  • have you added "libz.1.2.5.dylib" framwork in your app ? – Santosh Sharma Sep 08 '14 at 13:03
  • Please go through http://stackoverflow.com/questions/14171111/cocoa-error-3840-using-json-ios and http://stackoverflow.com/questions/23147950/error-domain-nscocoaerrordomain-code-3840-the-operation-couldn-t-be-completed – Santosh Sharma Sep 08 '14 at 13:05
  • thanks, Ive added "libz.1.2.5.dylib" already, still same error. – Bordz Sep 08 '14 at 13:34
0
-(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 );

}
Franco
  • 19
  • 2