Method: Browser::HTTP::Request#send

Defined in:
opal/browser/http/request.rb

#send(parameters = @parameters) ⇒ Response

Send the request with optional parameters.

Parameters:

  • parameters (String, Hash) (defaults to: @parameters)

    the data to send

Returns:

[View source]

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'opal/browser/http/request.rb', line 261

def send(parameters = @parameters)
  raise 'the request has not been opened' unless opened?

  raise 'the request has already been sent' if sent?

  # try to circumvent caching setting an If-Modified-Since header with a very
  # old date
  unless cacheable?
    `#@native.setRequestHeader("If-Modified-Since", "Tue, 11 Sep 2001 12:46:00 GMT")`
  end

  @headers.each {|name, value|
    `#@native.setRequestHeader(#{name.to_s}, #{value.to_s})`
  }

  if @content_type
    header  = @content_type
    header += "; charset=#{@encoding}" if @encoding

    `#@native.setRequestHeader('Content-Type', header)`
  end

  if binary?
    if Buffer.supported?
      `#@native.responseType = 'arraybuffer'`
    else
      `#@native.overrideMimeType('text/plain; charset=x-user-defined')`
    end
  end

  if mime_type && !binary?
    `#@native.overrideMimeType(#@mime_type)`
  end

  @sent     = true
  @response = Response.new(self)

  if String === parameters
    data = parameters
  elsif (Hash === parameters && !parameters.empty?) || FormData === parameters
    data = if Hash === parameters
      if FormData.contain_files?(parameters)
        FormData.build_form_data(parameters)
      else
        FormData.build_query(parameters)
      end
    else #if FormData === parameters
      parameters
    end

    unless @content_type
      if FormData === data
        # I thought it's done this way, but it isn't. It actually is
        # "multipart/form-data; boundary=-----------.......". Let's miss it
        # purposefully, because it's filled in automatically in this example.
        # `#@native.setRequestHeader('Content-Type', 'multipart/form-data')`
      else
        `#@native.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')`
      end
    end

    data = data.to_n
  else
    data = `null`
  end

  `#@native.send(#{data})`

  @response
end