Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 16th, 2007, 10:25 AM
Boldgeek
Guest
 
Posts: n/a
Default Trying to submit multipart/form-data HTTP POST using PHP script

I am trying to develop an app that will allow automatic updating of a
web form which uses multipart/form-data enctype (as it MIGHT be sending
an image)

I have an example form, which when submitted displays the results as
you would expect.
However when I send the same data using the PHP script I get a 400
error:

Bad Request
Your browser sent a request that this server could not understand.
Request header field is missing colon separator.
( it then displays the boundary string.)

After examining the HTTP headers for the submitted form, it appears
EXACTLY the same as the post I make via the PHP script. I've tried
copying EVERYTHING exactly and it still comes up with the error.

If I remove the actual content, ie end the request like:

Content-Type: multipart/form-data;
boundary=---------------------------265001916915724
Content-Length: 0

It works fine. However as soon as we start adding boundary strings and
form parts, I get the error. Everything is formatted correctly. I can
provide examples if required.

I'm using fsockopen in the PHP script to connect to the target server &
page

I'm at my wits end trying to see what is wrong with the PHP script -
anyone got any ideas?
Thanks in advance...

  #2  
Old January 16th, 2007, 07:45 PM
Rik
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script

Boldgeek wrote:
Quote:
I am trying to develop an app that will allow automatic updating of a
web form which uses multipart/form-data enctype (as it MIGHT be
sending an image)
>
I have an example form, which when submitted displays the results as
you would expect.
However when I send the same data using the PHP script I get a 400
error:
>
Bad Request
Your browser sent a request that this server could not understand.
Request header field is missing colon separator.
( it then displays the boundary string.)
>
After examining the HTTP headers for the submitted form, it appears
EXACTLY the same as the post I make via the PHP script. I've tried
copying EVERYTHING exactly and it still comes up with the error.
>
If I remove the actual content, ie end the request like:
>
Content-Type: multipart/form-data;
boundary=---------------------------265001916915724
Content-Length: 0
>
It works fine. However as soon as we start adding boundary strings and
form parts, I get the error. Everything is formatted correctly. I
can provide examples if required.
>
I'm using fsockopen in the PHP script to connect to the target server
& page
>
I'm at my wits end trying to see what is wrong with the PHP script -
anyone got any ideas?
As my crystal ball is broken, I cannot see what is wrong with either the
request you send, or with the PHP code that builds it. If you really want
to know what's wrong, post an example of the request that breaks (offcourse
strip out the actual file-data, and possibily security stuff), not the
request that's OK :-)

What I can advise you though, is perhaps to use curl instead of fsockopen
if you have it available. It saves a lot of handcoding requests.
--
Rik Wasmus


  #3  
Old January 16th, 2007, 08:15 PM
Tim Roberts
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script

"Boldgeek" <ap@mardona.comwrote:
Quote:
>I am trying to develop an app that will allow automatic updating of a
>web form which uses multipart/form-data enctype (as it MIGHT be sending
>an image)
>
>I have an example form, which when submitted displays the results as
>you would expect.
>However when I send the same data using the PHP script I get a 400
>error:
>
>Bad Request
>Your browser sent a request that this server could not understand.
>Request header field is missing colon separator.
>( it then displays the boundary string.)
>
>After examining the HTTP headers for the submitted form, it appears
>EXACTLY the same as the post I make via the PHP script. I've tried
>copying EVERYTHING exactly and it still comes up with the error.
>
>If I remove the actual content, ie end the request like:
>
>Content-Type: multipart/form-data;
>boundary=---------------------------265001916915724
>Content-Length: 0
Is the "boundary" parameter on the same line as the Content-Type, or at
least indented on the next line?
Quote:
>It works fine. However as soon as we start adding boundary strings and
>form parts, I get the error. Everything is formatted correctly. I can
>provide examples if required.
>
>I'm using fsockopen in the PHP script to connect to the target server &
>page
>
>I'm at my wits end trying to see what is wrong with the PHP script -
>anyone got any ideas?
You haven't given us enough information to know what the problem is. Here's
an example of a correct POST:

POST /cgi-bin/xxxx HTTP/1.0
Content-Type: multipart/form-data; boundary=----xxxx----

------xxxx----
Content-Disposition: form-data; name="myname"

Tim Roberts
------xxxx----
Content-Disposition: form-data; name="email"

timr@nonono.com

Is that what you are sending?
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  #4  
Old January 16th, 2007, 08:25 PM
Rik
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script

Rik wrote:
Quote:
As my crystal ball is broken
(c) some other dude offcourse... :P
--
Rik Wasmus


  #5  
Old January 16th, 2007, 09:55 PM
Boldgeek
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script

Here is the HTTP request sent by the app:

POST testresults.php HTTP/1.1
Host: www.(mydomain).org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)
Gecko/20061204 Firefox/2.0.0.1
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-gb,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data;
boundary=---------------------------265001916915724
Content-Length: 364
-----------------------------265001916915724
Content-Disposition: form-data; name="name"

test2
-----------------------------265001916915724
Content-Disposition: form-data; name="description"

This is a test
-----------------------------265001916915724
Content-Disposition: form-data; name="cmd"

Submit
-----------------------------265001916915724--



I've copied this as exactly as possible from the HTTP request sent by
the forms version (I used a Firefox plugin called Live HTTP Headers to
capture the headers) Note: There is a blank line at the end after the
final boundary.

The server response is :
Bad Request
Your browser sent a request that this server could not understand.
Request header field is missing colon separator.


-----------------------------265001916915724


Below is the HTTP request submitted by the form (copied from Live HTTP
Headers) which works fine.

POST /testresults.php HTTP/1.1
Host: www.(mydomain).org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)
Gecko/20061204 Firefox/2.0.0.1
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-gb,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.(mydomain).org/testform.php
Content-Type: multipart/form-data;
boundary=---------------------------77877924991
Content-Length: 364
-----------------------------77877924991
Content-Disposition: form-data; name="name"

form test
-----------------------------77877924991
Content-Disposition: form-data; name="description"

this is a test of the form
-----------------------------77877924991
Content-Disposition: form-data; name="cmd"

Submit
-----------------------------77877924991--


Now the only differences I can detect are the referrer (which has no
relevance to this particular application) and the boundary string. As a
test previously I've copied the same boundary string just to be certain
and there are no differences. Each line is terminated by "\r\n" as it
should be.

Thanks guys, your help is much appreciated.

  #6  
Old January 16th, 2007, 10:05 PM
Rik
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script

Boldgeek wrote:
Quote:
Here is the HTTP request sent by the app:
>
POST testresults.php HTTP/1.1
Host: www.(mydomain).org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.1)
Gecko/20061204 Firefox/2.0.0.1
Accept:
>
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q
=0.8,image/png,*/*;q=0.5
Quote:
Accept-Language: en-gb,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data;
boundary=---------------------------265001916915724
Content-Length: 364
And here there should be a blank line..........
Quote:
-----------------------------265001916915724
Content-Disposition: form-data; name="name"
>
test2
-----------------------------265001916915724
Content-Disposition: form-data; name="description"
>
This is a test
-----------------------------265001916915724
Content-Disposition: form-data; name="cmd"
>
Submit
-----------------------------265001916915724--

Hope that's it.
--
Rik Wasmus


  #7  
Old January 16th, 2007, 10:15 PM
Boldgeek
Guest
 
Posts: n/a
Default Re: Trying to submit multipart/form-data HTTP POST using PHP script


Rik wrote:
Quote:
Boldgeek wrote:
Quote:
Here is the HTTP request sent by the app:

POST testresults.php HTTP/1.1
Host: www.(mydomain).org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.1)
Gecko/20061204 Firefox/2.0.0.1
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q
=0.8,image/png,*/*;q=0.5
Quote:
Accept-Language: en-gb,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data;
boundary=---------------------------265001916915724
Content-Length: 364
>
And here there should be a blank line..........
>
Quote:
-----------------------------265001916915724
Content-Disposition: form-data; name="name"

test2
-----------------------------265001916915724
Content-Disposition: form-data; name="description"

This is a test
-----------------------------265001916915724
Content-Disposition: form-data; name="cmd"

Submit
-----------------------------265001916915724--
>
>
Hope that's it.
--
Rik Wasmus
Dear God! That was exactly the problem... I completely forgot the blank
line (which has been filtered out of the Live HTTP Header output as
well)

Thank you for that - I've been tearing my hair out for a couple of days
because of that!

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles