www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - HTTP() from std.net.curl hidden state

reply "Ilya Korobitsyn" <ilya-korobitsyn yandex.ru> writes:
Hello!

I am using HTTP structure to perform calls to a simple REST API.
Like this:

class API
{
      HTTP http;
      this() { http = HTTP(); }
      void call() {
           http.url = "some url";
           http.method = POST;
           http.setPostData("data", "type");
           http.perform();
      }
}

My issue is that I have to reinitialize http with HTTP() every 
call(), otherwise some state keeps on adding. For example, 
headers Content-Type keep on multiplying, and after setting 
method to DELETE it can not be changed to POST again (request 
still sends as delete).

Reinitializing http seems fine, however I suspect that something 
is lost (DNS cache, from example). Is there any workaround?

Thank you.

Example (without reinitialization):
3 subsequent requests with verbose curl, >> lines are what is 
desired, << received, rest is curl debug output.

 POST http://localhost:9515/session
 {"desiredCapabilities":{},"requiredCapabilities":{}}
* Hostname was NOT found in DNS cache * Trying 127.0.0.1...
 POST /session HTTP/1.1
User-Agent: Phobos-std.net.curl/2.067 (libcurl/7.35.0) Host: localhost:9515 Accept: */* Content-Type: application/json;charset=UTF-8 Content-Length: 52 * upload completely sent off: 52 out of 52 bytes < HTTP/1.1 200 OK < Content-Length:550 < Content-Type:application/json; charset=utf-8 < Connection:close < * Closing connection 0 << 200 {"sessionId":"d833e91674e2d14d2a81133821ab76fd","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"userDataDir":"/tmp/.com.google.Chrome.AlM0Ze"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"platform":"Linux","rotatable":false,"takesHeapSnapshot":true,"takesScreenshot":true,"version":"41.0.2272.76","webStorageEnabled":true}}
 DELETE 
 http://localhost:9515/session/d833e91674e2d14d2a81133821ab76fd
* Hostname was found in DNS cache * Trying 127.0.0.1...
 DELETE /session/d833e91674e2d14d2a81133821ab76fd HTTP/1.1
User-Agent: Phobos-std.net.curl/2.067 (libcurl/7.35.0) Host: localhost:9515 Accept: */* Content-Type: application/json;charset=UTF-8 Content-Type: text/plain Content-Length: 0 < HTTP/1.1 200 OK < Content-Length:72 < Content-Type:application/json; charset=utf-8 < Connection:close < * Closing connection 1 << 200 {"sessionId":"d833e91674e2d14d2a81133821ab76fd","status":0,"value":null}
 POST http://localhost:9515/session
 {"desiredCapabilities":{},"requiredCapabilities":{}}
* Hostname was found in DNS cache * Trying 127.0.0.1...
 DELETE /session HTTP/1.1
User-Agent: Phobos-std.net.curl/2.067 (libcurl/7.35.0) Host: localhost:9515 Accept: */* Content-Type: application/json;charset=UTF-8 Content-Type: text/plain Content-Type: application/json;charset=UTF-8 Content-Length: 52 * upload completely sent off: 52 out of 52 bytes < HTTP/1.1 404 Not Found < Content-Length:24 < Content-Type:text/plain < Connection:close < * Closing connection 2 << 404 unknown command: session
Mar 26 2015
parent reply "Ilya Korobitsyn" <ilya-korobitsyn yandex.ru> writes:
It looks like in perform() method curl option is set:

p.curl.set(CurlOption.customrequest, "DELETE");

but is never reset and all requests after DELETE are DELETE too 
(even if asked for POST).
Mar 27 2015
parent "Ilya Korobitsyn" <ilya-korobitsyn yandex.ru> writes:
Solved by a workaround:

// before anything else in method call()
http.handle.set(CurlOption.customrequest, cast(void*) null);
http.clearRequestHeaders();

Hope this will help is anyone else has this problem.
Mar 27 2015