1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Empty values for the request.

Discussion in 'iPhone/iPad Programming' started by Alhazred, Jul 13, 2011.

  1. macrumors member

    [richiesta setHTTPBody:[[NSString stringWithFormat:@"macaddress=%@&user=%@&titolo=%@&trama=%@",appDelegate.macaddress,appDelegate.nomeutente,self.titolo.text,self.trama.text] dataUsingEncoding:NSASCIIStringEncoding]];
    NSLog(@"Mac: %@ \n\n Titolo: %@ \n\n Trama: %@",appDelegate.macaddress,self.titolo.text,self.trama.text);
    Why the first line sends emty values to the php script, while the second prints them correctly?

    I've tied to put strings in the request to the php script and they arrives, so the code for the request is correct.
    I do not understand...
  2. macrumors 65816


    Are you trying to send a GET or POST request? HTTP bodies don't apply to GET requests, the parameters need to encoded into the URL for GET requests. If you attempting a POST request, you need set that up with setHTTPMethod:.
  3. macrumors member

    I'm using POST
    NSMutableURLRequest *richiesta = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sito.it/iosphp/trame.php"]
    														 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [richiesta setHTTPMethod:@"POST"];
    as said if I use something like this the request works:
    NSString *mac = @"2323232";
    NSString *user = @"user";
    NSString *tit = @"titolo";
    NSString *tra = @"trama";
    [richiesta setHTTPBody:[[NSString stringWithFormat:@"macaddress=%@&user=%@&titolo=%@&trama=%@",mac,user,tit,tra] dataUsingEncoding:NSASCIIStringEncoding]];
  4. macrumors member

    It looks that the problem is caused by the presence of some special character such as '
    Those characters are present inside the database text but not shown on the iPad display and they cause the malfunction, if there are no ' in the text, the request works correctly.
    I do I have to manage those characters?
  5. macrumors member

    Solved the problem with ´ that it is different from ', I've changed them inside the database.
    Now the problem are the characters like è à ù ì ò é

    Using NSUTF8StringEncoding the request is sent to the script and it reads the parameters, but those characters are read not correctly.

    How to correctly manage those characters?
  6. macrumors 603

  7. macrumors member

    I see, but shouldn't that be done by dataUsingEncoding:NSUTF8StringEncoding used for the NSMutableURLRequest object?

    There are also other characters which need to be percent-encoded, such as ^ or | and they are correctly sent doing nothing.
  8. macrumors 603

    How would the NSMutableURLRequest know what to encode and what to leave alone?

    You're passing it a sequence of bytes that looks something like this:
    It's not going to percent-encode that, because it doesn't know which parts are literal (the &'s and ='s) and which parts aren't.

    You need to percent-encode your parameter values BEFORE you use them with stringWithFormat:. There is an NSString method for percent encoding. Look it up.

    Imagine what will happen if your titolo and trama values are @"k=35%" and @"B&W". The expanded result would be:
    Get a tool like WireShark and look at the data in the POST request. See if it's what you expect.

    Just because those happen to work doesn't mean it's correct. All it means is that your POST body didn't happen to use those for a reserved purpose.
  9. macrumors member

    Before to pass the strings to setHTTPBody
    I've parsed them with

    same result.
  10. macrumors 603

    Post your actual code. We can't debug isolated fragments or descriptions. We need to see the exact code you're using.

    Consider the possibility that I may want to compile it and test it here.

    Apply basic testing and debugging skills.

    Consider making a small test program that uses specific test cases that you know will work. Then make a small change to it that introduces a problematic character, such as single-quote or one of your accented letters. Retest.

    In short, make a program that you know works; test it; then make the smallest change that causes it to fail.
  11. macrumors member

    This is the code
    - (void)editTramaDone:(id)sender {
    	[trama resignFirstResponder];
    	TabularRecInfoAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    	NSString *parsedUser = [appDelegate.nomeutente stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    	NSString *parsedTitolo = [self.titolo.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    	NSString *parsedTrama = [self.trama.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    	NSMutableURLRequest *richiesta = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sito.it/iosphp/trame.php"]
    															 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    	[richiesta setHTTPMethod:@"POST"];
    	[richiesta setHTTPBody:[[NSString stringWithFormat:@"user=%@&mac=%@&titolo=%@&trama=%@", parsedUser, appDelegate.macaddress, parsedTitolo, parsedTrama] dataUsingEncoding:NSUTF8StringEncoding]];
    	NSURLResponse *response;
    	NSError *error;
    	NSData *dati = [NSURLConnection sendSynchronousRequest:richiesta returningResponse:&response error:&error];
    	NSString *esitoModifica = [[[NSString alloc] initWithData:dati encoding:NSASCIIStringEncoding] autorelease];
    	self.navigationItem.rightBarButtonItem = nil;
  12. macrumors 603

    What have you done to debug this? Be specific.

    Have you NSLog'ed intermediate data, such as the individual percent-escaped strings? If so, post the actual NSLog output.

    Exactly what is the value of appDelegate.macaddress, which is not being percent-escaped? Post its actual NSLog output.

    Have you broken out the data before it's set as HTTP body and NSLog'ed that? Post the actual NSLog output.

    For example, I would want to see the formatted string here, BEFORE it's converted to NSData:
    setHTTPBody:[[NSString stringWithFormat:@"user=%@&mac=%@&titolo=%@&trama=%@", parsedUser, appDelegate.macaddress, parsedTitolo, parsedTrama] dataUsingEncoding:NSUTF8StringEncoding]
    Have you dumped the bytes of the NSData as hex and confirmed that they are all valid, and the structure is well-formed (no extra bytes, no null bytes)? What does an NSLog of the NSData produce? Post the output.

    Have you used the debugger to set breakpoints, and examined the data at each point?

    You've described nothing about the HTTP response.

    What output is produced by NSLog'ing the NSURLResponse's significant properties?

    What output is produced by NSLog'ing the NSData?

    What output is produced by NSLog'ing the NSData converted to string?

    Effective debugging requires you to break things down into smaller pieces, and confirm that the smaller pieces are correct. You've barely begun to really break this down into smaller pieces, and you haven't posted any NSLog output.

    We can't see your screen. All we know about this is what you tell us.
  13. macrumors member

    Problem solved, in the code I sent the request using UTF8 while I was reading the response with ASCII encoding, so I thought that also the script where reading the parameters in a wrong way xD

    Thanks for your help. :)

Share This Page