2

I have changed over to NSURLConnection and NSURLRequest delegates for my db connections etc. I was using the ASIHTTPRequest libraries to do all this stuff but finally decided to move on due to the lack of support for that 3rd party library.

what I am woundering is how do I send post requests to my db like you do with the ASIFormDataRequest as shown below

//This sets up all other request
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        [request setDelegate:self];
        [request setValidatesSecureCertificate:NO];
        [request setPostValue:@"ClientDataSet.xml" forKey:@"filename"];  
        [request startSynchronous];

i.e. how to do send the setPostValues with the NSURLRequest class? any help would be greatly appreciated

C.Johns
  • 10,185
  • 20
  • 102
  • 156

1 Answers1

5

Using NSURLRequest is slightly more difficult than utilizing ASIHTTPRequest. You have to build your own post body.

NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL]; 
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postBodyData];
fscheidl
  • 2,281
  • 3
  • 19
  • 33
  • awesome! I can see what your doing there.. thats mint.. With regard to the postBodyString being UTF8String.. one of the requirements is for me to send the data in utf8 format but without the leading utf8 prefix? – C.Johns Feb 22 '12 at 01:29
  • `postBodyString` itself is no `UTF8String`. It is only converted to one. – fscheidl Feb 22 '12 at 01:31
  • okay cool thanks very much for that code example.. this has helped greatly. – C.Johns Feb 22 '12 at 01:39
  • question is setHTTPBody Https compatible – C.Johns Feb 22 '12 at 02:55