Archived Forum Post

Index of archived forum posts

Question:

rest.ResponseStatusCode = -1

May 17 '17 at 12:51

Hello,

I have not 100% tested this, but this is the behaviour I'm seeing, making me think it is a possible bug.

Request: http://www.somesite.com/something.html
------------------------------------------------
Response:
Date: Sat, 13 May 2017 10:02:08 GMT
Set-Cookie: lang=english; domain=.somesite.com; path=/
Set-Cookie: code=12345abcde; domain=.somesite.com; path=/; expires=Sun,
14-May-2017 10:02:08 GMT
Location: //www.somesite.com/foebar.php
Connection: close
Transfer-Encoding: chunked
Content-Type: application/cgi
------------------------------------------------
rest.ResponseStatusCode = 302
rest.RedirectUrl() ==> http://www.somesite.com/foebar.php
Request: http://www.somesite.com/foebar.php
NOTE: Cookie NOT set
------------------------------------------------
Response:
exactly the same as above (except different times)
------------------------------------------------
rest.ResponseStatusCode = -1
rest.RedirectUrl() ==> http://www.somesite.com/foebar.php

I expect an error text, due to ResponseStatusCode = -1. However:

ChilkatLog:
  RedirectUrl:
    DllDate: May 12 2017
    ChilkatVersion: 9.5.0.67
    UnlockPrefix: *REMOVED*
    Architecture: Little Endian; 64-bit
    Language: .NET 4.6 / x64
    VerboseLogging: 1
    url: http://www.somesite.com/foebar.php
    Success.
  --RedirectUrl
--ChilkatLog

So. My assumption is that the responseStatusCode is set to -1 due to the fact that the new location URL is the same as the previous. Ifso, that is a bug. The page was correctly loaded and thus statuscode 302 should be given (in this situation). Hooever, perhaps with a remark in the verbose logging that the new URL is the same as the previous one (This is the end-user's problem)

In the above example, the server wants you to set a cookie, if not it keeps requesting you to do so untill you ave set it.

Which brings me to the request if it is possible to add cookie support (that is available in the HTTP module) to the REST module? Clearly I did not build my cookie integration very well, causing the problem to surface... I found that this has been requested before: http://www.chilkatforum.com/questions/11395/cookie-support-in-rest-module


EDIT After fixing my cookiehandler, the problem remained. Upon further investigation I now get the error

responseHeader: [2ef9
Set-Cookie: ....and allot more header...
]
    Unrecognized 1st response line.
    firstLine: 2ef9
    Failed.
  --ReadResponseHeader
--ChilkatLog

So, I guess the question changes to:

When calling rest.ReadResponseHeader(); gives an error, why is the field rest.ResponseHeader not being updated?


Accepted Answer

The Chilkat REST API provides the flexibility to break up a single HTTP request/response into separate method calls. For example, you can send a POST and get the entire response header + body in a single call like this:

string responseBody = rest.FullRequestFormUrlEncoded(httpVerb,uriPath);
or you could break it up into separate calls:
bool success = rest.SendReqFormUrlEncoded(httpVerb,uriPath);
// check for success/failure...
int statusCode = rest.GetResponseHeader();
string responseBody = rest.ReadRespBodyString();

If you choose to do the latter, then you must read the response body after reading the header (because it's coming whether you want it or not). I think what you're doing is reading the response header, and then seeing that it's a 302 redirect, you forget to read the response body. You can see from the response header that a body in the "chunked encoding" is coming:

Date: Sat, 13 May 2017 10:02:08 GMT
Set-Cookie: lang=english; domain=.somesite.com; path=/
Set-Cookie: code=12345abcde; domain=.somesite.com; path=/; expires=Sun,
14-May-2017 10:02:08 GMT
Location: //www.somesite.com/foebar.php
Connection: close
Transfer-Encoding: chunked
Content-Type: application/cgi
If you neglect to read the response body, and then send another HTTP request (on the same connection), then when you read the response header, the data you'll get is the chunked response body from the previous request.