473,721 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...

Jan 16 '07 #1
6 32794
Rik
Boldgeek wrote:
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
Jan 16 '07 #2
"Boldgeek" <ap@mardona.com wrote:
>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?
>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"

ti**@nonono.com

Is that what you are sending?
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 16 '07 #3
Rik
Rik wrote:
As my crystal ball is broken
(c) some other dude offcourse... :P
--
Rik Wasmus
Jan 16 '07 #4
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,ima ge/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="descripti on"

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,ima ge/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="descripti on"

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.

Jan 16 '07 #5
Rik
Boldgeek wrote:
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
And here there should be a blank line..........
-----------------------------265001916915724
Content-Disposition: form-data; name="name"

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

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

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

Hope that's it.
--
Rik Wasmus
Jan 16 '07 #6

Rik wrote:
Boldgeek wrote:
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

And here there should be a blank line..........
-----------------------------265001916915724
Content-Disposition: form-data; name="name"

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

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!

Jan 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1930
by: Christoph Weber | last post by:
Hello, I have the problem that I want to submit a form only if the submit button is clicked with the mouse. The form should not be submitted if the user hits the RETURN key. Any suggenstions? Thanks Christoph
8
3050
by: Matt | last post by:
I want to submit the form to the server without opening another page. When we do the following, it will submit the form data in myform to the IIS, and open page2.asp. <form name="myform" action="page2.asp" method="post"> But I don't want to open another page, I just want to submit the form data. Should I do the following?? myform.submit();
7
12445
by: Marco Alting | last post by:
Hi, I want to use the submit button of a new window to submit the form of the new window (which carries two values to the opener window) and then submit the opener form. Can anyone tell me how to do this?
3
11678
by: Megha Vishwanath | last post by:
Hi, I have a struts form <html:form> in which although the focus rests on the submit button, hitting the enter key does not submit the form. I tried capturing the Enter key event and followed it by the document.form.submit() but this takes me through the validation attached with the submit twice( giving me all the alerts twice). Here's the code
6
18829
by: HD | last post by:
Hi. I have an asp page with radio buttons and a combobox... when the user clicks a radio button, I want the form to submit so I can execute the ASP code in order to change the list shown in the combobox... I have 2 radiobuttons: <input type="radio" name="terriprod" value="E">E <input type="radio" name="terriprod" value="D">D I have tried adding an OnClick event to each of the radio button...
4
2818
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow the user to select multiple files. As you may know, the tag produces a text input field with an adjacent button for selecting a file from the local file system. As I would like to be able to allow the user to upload an arbitrary number of files...
5
4341
by: ningjun.wang | last post by:
How can I submit a form to multiple web sites when user click the submit button? Something like the following: myform.action = url_of_server1; myform.submit(); myform.action = url_of_server2; myform.submit(); The above code only submit form to url_of_server1. The second submit is ignored. Is there any trick to make it works? For example is it
9
5481
by: shiks1305 | last post by:
Hi all, I want to submit a form after confirmation through a customised window. I have 4 buttons in <af:form>, one of which is continue button that has to navigate the form to another page while executing the action: -------------------------------------------------------- <af:commandButton id="continueBtn" onclick="return NewWindow()" action="#{CoverageMPB.process}" text="continue" />...
12
6943
by: Daniel Klein | last post by:
I'm pretty new at php and web stuff so please be gentle with me. I'm trying to get a form to submit when the user presses the Enter key. I do not want to use javascript. I've googled this to death and cant find the answer (only hints), except on the 'Experts Exhange' page and you have to pay to get the answer :-( Why is this such a secret in the open source world we live in? Daniel Klein
4
2958
by: Daniel Smedegaard Buus | last post by:
Hey :) I'm trying to figure this one out, and it's a bit tricky. I have a "tip a friend" form that's submitted via AJAX. When the user presses enter inside one of the form's input fields, the form should submit. This works out-of-the-box in FF and Safari, probably others too, but not in IE6. This is why I've used the "if(event.keyCode == 13) { try
0
8852
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5992
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3206
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.