0

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.
   -->
Vincent Monteil
  • 568
  • 5
  • 22
  • 1
    may have a typo here: Content-Disposition: from-data; ...did you want form-data? – gro Jun 09 '14 at 19:28

2 Answers2

0

I don't have a ready answer to your problem but if you watch closely the headers you send are very similar to those of an upload form in HTML, because basically that's what they are. If you are debugging these kind of problems from within an app you're wasting a lot of time restarting your simulator / debugging device over and over.

I would suggest that you try to fix your uploads first using a simple upload form that posts to a dynamic page. What kind of server you're running isn't clear but I imagine it has PHP support. So create form.html and let is post to file.php. Make sure that works. If that doesn't work you know the hosting company has some problems.

Next step is to figure out the headers it sends using Firebug or better network debugging tools. Working headers for form submits are always working headers because the receiving script file doesn't care where it comes from. These headers should work in your app as well.

A thing I saw happening a lot with servers is that they're not using the same string encoding you are using. You could figure out what the string encoding is by checking out the text documents like HTML pages it serves to you when you don't specify any encoding yourself. UTF8 should be OK but I had to resort to Windows1252 on some occasions. Imagine some vital header field having corrupted special chars, that wouldn't work.

Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
0

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 !

Vincent Monteil
  • 568
  • 5
  • 22