-5

Possible Duplicate:
iPhone SDK Xcode 4.2 iOS 5- How do i send a json to url ? (POST not GET) [Resolved]

    NSDictionary *requestDic=[NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSString *jsonreq=[requestDic JSONRepresentation];
    NSURL *requrl=[NSURL URLWithString:[NSString stringWithFormat:@"%@index.php?r=%@&te=%@&noCheck=1&checkInstall=start",[Info serverUrl],[dictParameters objectForKey:@"r"],[dictParameters objectForKey:@"te"]]];
    requrl = [NSURL URLWithString:[[requrl absoluteString] stringByAppendingString:subUrl]];
    NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:requrl];
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setValue:@"application/json-x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSData *requestData=[NSData dataWithBytes:[jsonreq UTF8String] length:[jsonreq length]];
    [req setHTTPBody:requestData];
    [browser loadRequest:req]
Community
  • 1
  • 1

3 Answers3

1

Dilshand's answer is ok , but you can use ASIFormDataRequest for ease. like follow

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:mobileApplicationId forKey:@"mobileApplicationId"];
[request setPostValue:mobileApplicationVersion forKey:@"mobileApplicationVersion"];
[request setPostValue:userID forKey:@"userId"];
[request setPostValue:pass forKey:@"password"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
0

What you want to post server give complete detail and what error you are getting

try following i have done using this to send data to server and it works fine for me hope this helps

    NSString *post =[[NSString alloc] initWithFormat:@"ProviderNPI=%@&PatientID=%@&FileURL=%@&FileTYPE=%@&DataSynID=%@& AppointmentListingsID=%@",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];


    NSURL *url=[NSURL URLWithString:@"http://myserver.com/Sync.php?"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);
0

NSURL *url = [NSURL URLWithString:@"http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0"];

NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@",jsonRequest);

NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

NSLog(@"%@",json_data);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];


[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding 
                      allowLossyConversion:YES]];

// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];

NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
                                  initWithRequest:request 
                                  delegate:self];

// Successful connection.
if (nsUrlConnection) {

   // [self initSpinner];
   // [self spinBegin];

    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData=data;
    [data release];
} 
// Unsuccessful connection.
else {

}  
// Clean up
[url release];
[request release];
// Close keypad.