iosobjective-chttppostasiformdatarequest

ASIFormDataRequest POST Request Issue


im using this code to post data to my server

NSURL *mainurl = [NSURL URLWithString:@"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];

    NSString * postdata = [[NSString alloc]initWithFormat:@"UniqueId=%@&jsonProduct=%@&BranchId=%@&OrderToTime=%@",GETUnicidentifire,JsonOrderDetail,BranchId,OrderToTime];

    ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];

    [requestt setRequestMethod:@"POST"];
    [requestt addRequestHeader:@"application/x-www-form-urlencoded" value:@"Content-Type"];
    [requestt appendPostData:[postdata dataUsingEncoding:NSUTF8StringEncoding]];

    NSString * theurl = [NSString stringWithFormat:@"%@",mainurl];
    NSURLRequest *thereqest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:theurl]];
    [NSURLConnection connectionWithRequest:thereqest delegate:self];

in the method

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"%@",error); 
}

im geting: {Message:The requested resource does not support http method 'GET'.}

what im doing Wrong ?


Solution

  • You are mixing things up here. You build an ASIFormDataRequest, but you don't actually send it. What you do send is an NSURLConnection.

    It's been a long time since I've used ASI, but this might help:

    NSURL *mainurl = [NSURL URLWithString:@"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];
    
    ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];
    
    [requestt addPostValue:GETUnicidentifire forKey:@"UniqueId";
    [requestt addPostValue:JsonOrderDetail   forKey:@"jsonProduct";
    [requestt addPostValue:BranchId          forKey:@"BranchId";
    [requestt addPostValue:OrderToTime       forKey:@"OrderToTime";
    
    [requestt setCompletionBlock:^{
        // Process the response
    }];
    [requestt setFailedBlock:^{
        NSError *error = [requestt error];
        NSLog(@"%@",error);
    }];
    
    [requestt startAsynchronous];
    

    As a word of advice, replace ASI with something like AFNetworking. ASI is no longer being developed.