Search code examples
iosxcodehttpnsmutableurlrequestbad-request

NSMutableURLRequest error Bad Header


I'm trying to send a picture to my server using HTTP POST on iOS 7/8. My problem is that I get an error on the request (pasted below) no matter which URL of my site is set. I sent an e-mail to my hosting company and they told me that it was due to a bad header. I can't figure out what's going wrong with my code... I have put the same boundary string than in the code examples on the web, mabe should I not have done it.

PLEASE HELP ME !

Live code :

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"uploadingpic.png"], 90);
    self.preview.image = [UIImage imageNamed:@"uploadingpic.png"];
    NSString *urlString=@"http://myCoolURL/upload.php";

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: from-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [request setHTTPBody:body];

     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
     NSString *returnString=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

     NSLog(returnString);

Here is the detailed error (specific to my servor provider i think) returned to NSLog :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>400 Bad Request</TITLE>
</HEAD>
<BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.
<P>
Client sent malformed Host header
<P>
<HR>
<H1>Mauvaise Requête</H1>
Votre navigateur a envoyé une demande que ce serveur ne peut pas comprendre.
<P>
Le client a envoyé une en tête malformé
<P>
<HR>
<H1>Solicitud incorrecta</H1>
Su navegador ha enviado una solicitud que el servidor no puede entender.
<P>
El cliente ha enviado un encabezado incorrecto.
<P>
<HR>
<ADDRESS>
</ADDRESS>
</BODY>
</HTML>

<!--
   - Unfortunately, Microsoft has added a clever new
   - "feature" to Internet Explorer. If the text of
   - an error's message is "too small", specifically
   - less than 512 bytes, Internet Explorer returns
   - its own error message. You can turn that off,
   - but it's pretty tricky to find switch called
   - "smart error messages". That means, of course,
   - that short error messages are censored by default.
   - IIS always returns error messages that are long
   - enough to make Internet Explorer happy. The
   - workaround is pretty simple: pad the error
   - message with a big comment like this to push it
   - over the five hundred and twelve bytes minimum.
   - Of course, that's exactly what you're reading
   - right now.
   -->

Solution

  • Someone spotted my error on apple devforums. If you check my code, I have typed "from" somewhere rather than "form" Such a small mistake ^^'

    Thank you anyway !