How do you use 'setRequestHeader()' with POST ?

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Hi,

Hopefully I've made no mistake posting in this group.
I'd like to post an XML request to a server, using
~ http = new XMLHttpRequest();
~ http.setRequestHeader(...);
~ var myXmlString = "<?xml version=... <get-stuff-command...";
~ http.send(myXmlString);

but I'm not sure of what I have to set with setRequestHeader().
Tried to find a page where all options are described, to no avail.
Could someone please tell me what I have to set with this
method ? And if there's a page somewhere where it's clearly
described ?

In advance, thanks a lot.
Regards,
Sébastien
Brussels
 
T

Thomas 'PointedEars' Lahn

Sébastien de Mapias said:
Hopefully I've made no mistake posting in this group.

Too late :) You should have included relevant information
to your question (here: POST) in the message body, too.
I'd like to post an XML request to a server, using
~ http = new XMLHttpRequest();

Code prefixes like `~' are confusing, don't do that. That aside, you should
declare `http':

var http = ...;
~ http.setRequestHeader(...);
~ var myXmlString = "<?xml version=... <get-stuff-command...";
~ http.send(myXmlString);

You can't send() before you open() the connection:

http.open("POST", uri, trueForSync);
but I'm not sure of what I have to set with setRequestHeader().
Tried to find a page where all options are described, to no avail.
Could someone please tell me what I have to set with this
method ? And if there's a page somewhere where it's clearly
described ?

RTFM: <http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.3>

(Yes, HTML. Because HTTP POST started being widely used when people started
using HTML forms for transmitting large amounts of information [such as
files] that did not fit into browser's [_not_ specification's] -- URL length
limits.)

See also: <https://developer.mozilla.org/en/Ajax>


HTH

PointedEars
 
M

Martin Honnen

Sébastien de Mapias said:
I'd like to post an XML request to a server, using
~ http = new XMLHttpRequest();
~ http.setRequestHeader(...);
~ var myXmlString = "<?xml version=... <get-stuff-command...";
~ http.send(myXmlString);

but I'm not sure of what I have to set with setRequestHeader().
Tried to find a page where all options are described, to no avail.
Could someone please tell me what I have to set with this
method ? And if there's a page somewhere where it's clearly
described ?

Read the HTTP specification on which HTTP request headers exist
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14). If you
send XML (and don't pass an XML DOM document to the send method) then
you might want to set
http.setRequestHeader('Content-Type', 'application/xml');
 
S

Sébastien de Mapias

Well, to be more clear and precise: is it possible
to use the XMLHttpRequest object to post a request
just like you would do it to a servlet using Apache's
commons.httpclient.methods.PostMethod with the following
lines ?:
~ String uris = "http://localhost/app-server/ServletName";
~ PostMethod meth = new PostMethod();
~ meth.setURI(new URI(uris), true));
~ meth.setRequestBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?...");
~ int code = httpClient.executeMethod(meth);

Thanks.
 
S

Sébastien de Mapias

Ah sorry - I forgot to add that... some authentication
is required on my server => how do I pass my user/pwd
through the XMLHttpRequest object ?

Thanks...
 
T

Thomas 'PointedEars' Lahn

Sébastien de Mapias said:
Ah sorry - I forgot to add that... some authentication
is required on my server => how do I pass my user/pwd
through the XMLHttpRequest object ?

The same as in HTTP, if it even is supported. Read RFCs 1945 and 2616.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
You can't send() before you open() the connection:

http.open("POST", uri, trueForSync);

That should be

http.open("POST", uri, trueForAsync);

meaning that you need to pass

true

for the third argument if you want the request-response handling to be
asynchronous,

false

otherwise (not recommended). `true' is the documented default but I would
not rely on that.

BTW, you shouldn't call .setRequestHeader() or assign to .onreadystatechange
before you open(), either.


PointedEars
 
B

Bart Van der Donck

Sébastien de Mapias said:
Ah sorry - I forgot to add that... some authentication
is required on my server => how do I pass my user/pwd
through the XMLHttpRequest object ?

The 4th and 5th parameter of the 'open()'-call are meant for this,
e.g.

http.open('GET', 'mypage.htm', true, 'myusername', 'mypassword');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top