Archived Forum Post

Index of archived forum posts

Question:

How to have some params on the URL, and some in the application/x-www-form-urlencoded HTTP request body?

Sep 10 '12 at 13:39
$request = new CkHttpRequest();
$request->SetFromUrl($id);
$request->UsePost();
$request->AddParam('file_id',$idx);
$request->AddParam('password',’’);
$request->AddParam('submit','Show');

$response = $http->PostUrlEncoded('http://something.com/index.php?id=10', $request);
In this sample, 'http://something.com/index.php?id=10 is not working. Basically Chilkat removes ?id=10 (GET parameter)

How can i post data with GET?


Answer

I'll describe some very basic rules of the HTTP protocol.

1) Any HTTP request (and response) is composed of a "start line" followed by a MIME message. The "start line" contains the HTTP verb, such as "GET", "POST", "PUT", etc., the path of the resource being accessed, and the HTTP protocol version, such as "1.1". The MIME is composed of MIME headers followed by a MIME body (if not empty).

2) A GET request implies that the request params are URL encoded in the path on the "start line". A GET request implicitly has a 0-length MIME body, so you only have the "start line" followed by the MIME headers. This would be a valid GET request:

GET /index.php?id=10&file_id=1&password=abc&submit=Show HTTP/1.1
Accept: /
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Host: something.com

3) A POST request implies that the request parameters are provided in the MIME body of the request, not in the "start line". The "start line" provides the path to the resource. The server-side should process a request by obtaining the path to the resource (from the start-line), and then obtain the form parameters from the MIME. (However, if some form params are included w/ the path in the "start line", then these should be parsed as params and should not be considered as part of the path to the resource.) Here is an example of a straightforward and reasonable HTTP POST request that follows the rules of the HTTP protocol:

POST /index.php HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: something.com
Content-Length: 40

file_id=1&password=abc&submit=Show&id=10

What you are requesting is a mixture of the two. You're asking for a POST such that one form param is provided as part of the path within the "start line", and the remainder are included in the request's MIME body. You would like to send this:

POST /index.php?id=10 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost
Content-Length: 34

file_id=1&password=abc&submit=Show

Here we see that "id" is specified in the "start line", and the other form params are provided in the MIME body. The server-side should be parsing this exactly the same as in the case where the form params are all contained in the body, because the path to the resource is "/index.php". It is NOT "/index.php?id=10". In other words, in all three cases above, the resource should be identified as "/index.php", with 4 form params, the names of which are "id", "file_id", "password", and "submit".

Unfortunately, it must be that the server-side is incorrectly implemented because it deems the resource's path as "/index.php?id=10".

To get the desired HTTP request (where "id" is specified in the "start line") use SynchronousRequest instead:

$request = new CkHttpRequest();
$request->put_Path('/index.php?id=10');
$request->UsePost();
$request->AddParam('file_id',$idx);
$request->AddParam('password',’abc’);
$request->AddParam('submit','Show');

$response = $http->SynchronousRequest('something.com', 80, false, $request);