Skip to content

Instantly share code, notes, and snippets.

@hboon
Created January 28, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hboon/5dc789454242e65434681b937ebc47e9 to your computer and use it in GitHub Desktop.
Save hboon/5dc789454242e65434681b937ebc47e9 to your computer and use it in GitHub Desktop.
POST with NSURLSession in RubyMotion
def session
return @session if @session
configuration = NSURLSessionConfiguration.defaultSessionConfiguration.tap do |conf|
conf.HTTPAdditionalHeaders = {'Accept'=>'application/json',
'Accept-Language'=>'en',
'User-Agent'=> user_agent}
end
@session = NSURLSession.sessionWithConfiguration(configuration)
@session
end
def post_example(aURLString, aDictionary={}, &block)
aDictionary = aDictionary.mutableCopy
aDictionary['apikey'] = api_key
request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString("#{url_prefix_v2}#{aURLString}")).tap do |v|
v.HTTPMethod = 'POST'
end
data = NSJSONSerialization.dataWithJSONObject(aDictionary, options: 0, error: nil)
task = session.uploadTaskWithRequest(request, fromData: data, completionHandler: proc {|data, response, error|
handle_api_result(data, error, &block)
})
task.resume
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment