0

i'm trying to send an HTTP post request from my objective-c application to my server. i tried many different things, but i can't get the request to work with the content on request body. With the parameters on the url it work just fine, but i need to send a string with reserved characters.

This is my class responsible for the request:

#import "CLPUtilRequest.h"
static NSString *baseURL = @"http://localhost:8080/cl-mobile";

@implementation CLPUtilRequest

//Parameteres on the URL (working!!)
+ (NSData *) makeHttpRequest:(NSString *)url {
//Set info for webservice
NSString *urlString = baseURL;
urlString = [urlString stringByAppendingString:url];
NSURL *resquestUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:resquestUrl];

[request setHTTPMethod: @"POST"];
[request setValue:@"application/http" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

//Call the webservice
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
return [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
}

//Parameteres on the bodt (not working!!)
+ (NSData *) makeHttpRequestContentOnBody:(NSString *)url withContent:(NSString *)content {
NSData *postData = [content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
return data;
}

@end

And this is where i call the request.

NSString *contentString = @"chaveUsuarioEmpresa=";
contentString = [contentString stringByAppendingString:appDelegate.clinica.chave];
contentString = [contentString stringByAppendingString:@"&chaveAgendamento="];
contentString = [contentString stringByAppendingString:appDelegate.agendamento.chave];
contentString = [contentString stringByAppendingString:@"&anamnese="];
contentString = [contentString stringByAppendingString:self.texto.text];
NSData *dataResponse = [CLPUtilRequest makeHttpRequestContentOnBody:@"/agendamento/anamnese/save" withContent:contentString];

After calling the request, my errorReturned gives me the following (debugging): NSURLError * domain: @"NSURLErrorDomain" - code: -1002

I tried to do as described in this link (but didn`t make it): Sending an HTTP POST request on iOS

Thanks in advance!

Community
  • 1
  • 1
phellipeperin
  • 75
  • 1
  • 5

1 Answers1

0

according to Apple's Error Codes -1002 is the code for an unsupported url. and actually you are sending it to /agendamento/anamnese/save, as you don't prepend baseURL as you do in the other call

+ (NSData *) makeHttpRequest:(NSString *)url {
    //Set info for webservice
    NSString *urlString = baseURL;
    urlString = [urlString stringByAppendingString:url];
    NSURL *resquestUrl = [NSURL URLWithString:urlString];

while in makeHttpRequestContentOnBody:withContent: you use the unchanged passed in url string /agendamento/anamnese/save

Try

+ (NSData *) makeHttpRequestContentOnBody:(NSString *)url withContent:(NSString *)content
{
    NSData *postData = [content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSString *urlString = baseURL;
    urlString = [urlString stringByAppendingString:url];

    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *errorReturned = nil;
    NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
    return data;
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178