Class: Vertx::HttpClientRequest

Inherits:
Object
  • Object
show all
Includes:
WriteStream
Defined in:
lib/vertx/http.rb

Overview

Encapsulates a client-side HTTP request.

Instances of this class are created by an HttpClient instance, via one of the methods corresponding to the specific HTTP methods, or the generic Vertx::HttpClient#request method.

Once an instance of this class has been obtained, headers can be set on it, and data can be written to its body, if required. Once you are ready to send the request, the #end method must called.

Nothing is sent until the request has been internally assigned an HTTP connection. The HttpClient instance will return an instance of this class immediately, even if there are no HTTP connections available in the pool. Any requests sent before a connection is assigned will be queued internally and actually sent when an HTTP connection becomes available from the pool.

The headers of the request are actually sent either when the #end method is called, or, when the first part of the body is written, whichever occurs first.

This class supports both chunked and non-chunked HTTP.

Author:

Instance Method Summary collapse

Methods included from WriteStream

#_to_write_stream, #drain_handler, #exception_handler, #write, #write_queue_full?, #write_queue_max_size, #write_queue_max_size=

Constructor Details

#initialize(j_del) ⇒ HttpClientRequest

Returns a new instance of HttpClientRequest.



298
299
300
# File 'lib/vertx/http.rb', line 298

def initialize(j_del)
  @j_del = j_del
end

Instance Method Details

#chunked(val = nil) ⇒ Object

Get or set chunked



375
376
377
378
379
380
381
382
# File 'lib/vertx/http.rb', line 375

def chunked(val = nil)
  if val
    @j_del.setChunked(val)
    self
  else
    @j_del.getChunked
  end
end

#chunked=(val) ⇒ HttpClientRequest

Sets whether the request should used HTTP chunked encoding or not. will correspond to a new HTTP chunk sent on the wire. If chunked encoding is used the HTTP header ‘Transfer-Encoding’ with a value of ‘Chunked’ will be automatically inserted in the request. If chunked is false, this request will not use HTTP chunked encoding, and therefore if any data is written the body of the request, the total size of that data must be set in the ‘Content-Length’ header before any data is written to the request body.

Parameters:

  • val. (Boolean)

    If val is true, this request will use HTTP chunked encoding, and each call to write to the body

Returns:



369
370
371
372
# File 'lib/vertx/http.rb', line 369

def chunked=(val)
  @j_del.setChunked(val)
  self
end

#continue_handler(&hndlr) ⇒ Object

If you send an HTTP request with the header ‘Expect’ set to the value ‘100-continue’ and the server responds with an interim HTTP response with a status code of ‘100’ and a continue handler has been set using this method, then the handler will be called. You can then continue to write data to the request body and later end it. This is normally used in conjunction with the #send_head method to force the request header to be written before the request has ended.

Parameters:

  • hndlr. (Block)

    The handler



390
391
392
393
# File 'lib/vertx/http.rb', line 390

def continue_handler(&hndlr)
  @j_del.continueHandler(hndlr)
  self
end

#endObject

Ends the request. If no data has been written to the request body, and #send_head has not been called then the actual request won’t get written until this method gets called. Once the request has ended, it cannot be used any more, and if keep alive is true the underlying connection will be returned to the Vertx::HttpClient pool so it can be assigned to another request.



340
341
342
343
# File 'lib/vertx/http.rb', line 340

def end
  @j_del.end
  self
end

#headersObject

MultiMap of headers for the request



303
304
305
306
307
308
# File 'lib/vertx/http.rb', line 303

def headers
  if !@headers
    @headers = MultiMap.new(@j_del.headers)
  end
  @headers
end

#put_header(key, value) ⇒ HttpClientRequest

Inserts a header into the request.

Parameters:

  • key (String)

    The header key

  • value (Object)

    The header value. to_s will be called on the value to determine the actual String value to insert.

Returns:



314
315
316
317
# File 'lib/vertx/http.rb', line 314

def put_header(key, value)
  @j_del.putHeader(key, value.to_s)
  self
end

#send_headHttpClientRequest

Forces the head of the request to be written before #end is called on the request. This is normally used to implement HTTP 100-continue handling, see #continue_handler for more information.

Returns:



331
332
333
334
# File 'lib/vertx/http.rb', line 331

def send_head
  @j_del.sendHead
  self
end

#timeout(val = nil) ⇒ Object

Get or set timeout



396
397
398
399
400
401
402
403
# File 'lib/vertx/http.rb', line 396

def timeout(val = nil)
  if val
    @j_del.setTimeout(val)
    self
  else
    @j_del.getTimeout
  end
end

#write_buffer_and_end(chunk) ⇒ Object

Same as #end but writes some data to the response body before ending. If the response is not chunked and no other data has been written then the Content-Length header will be automatically set

Parameters:

  • chunk (Buffer)

    The Buffer to write



356
357
358
359
# File 'lib/vertx/http.rb', line 356

def write_buffer_and_end(chunk)
  @j_del.end(chunk._to_java_buffer)
  self
end

#write_str(str, enc = "UTF-8") ⇒ HttpClientRequest

Write a [String] to the request body.

Parameters:

  • str. (String)

    The string to write.

  • enc. (String)

    The encoding to use.

Returns:



323
324
325
326
# File 'lib/vertx/http.rb', line 323

def write_str(str, enc = "UTF-8")
  @j_del.write(str, enc)
  self
end

#write_str_and_end(str, enc = "UTF-8") ⇒ Object

Same as #write_buffer_and_end but writes a String

Parameters:

  • str (String)

    The String to write

  • enc (String) (defaults to: "UTF-8")

    The encoding



348
349
350
351
# File 'lib/vertx/http.rb', line 348

def write_str_and_end(str, enc = "UTF-8")
  @j_del.end(str, enc)
  self
end