Class: NSMutableURLRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/bean/nsmutableurlrequest_additions.rb

Overview

Extensions to the NSMutableRequest class

Class Method Summary collapse

Class Method Details

.post(url, opts) ⇒ NSMutableURLRequest

Create a POST request

Examples:

req = NSMutableURLRequest.post("https://example.com",
                               :body => "foo=1&bar=2&baz=3",
                               :head => { 'Content-Type' => 'application/x-www-form-urlencoded' })

Parameters:

  • url (String)

    The domain to post too

  • opts (Hash)

    Any options for the post

Options Hash (opts):

  • :body (String)

    The body to send with the POST request

  • :head (Hash)

    Any headers to send with the request

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bean/nsmutableurlrequest_additions.rb', line 16

def self.post(url, opts)
  req = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(url))
  req.setHTTPMethod('POST')

  if opts[:head]
    opts[:head].each_pair do |k, v|
      req.setValue(v, forHTTPHeaderField:k)
    end
  end

  req.setHTTPBody(opts[:body].to_data) if opts[:body]
  req
end