I am creating an iOS app using Rubymotion. I am trying to make a multipart upload of an image.
I use this code to make upload but I get error:
data = {token: "2xCGdzcuEeNzhst3Yaa8f", task: 1, message: "message", latitude: 1, longitude: 1}
client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/"))
request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data|
image_data = UIImagePNGRepresentation(image.image)
form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png')
})
operation = AFJSONRequestOperation.alloc.initWithRequest(request)
operation.setCompletionBlockWithSuccess(lambda{ |operation, responseObject| puts 'all done!'})
I get this error:
undefined method `setCompletionBlockWithSuccess' for #<AFJSONRequestOperation:0xb227f00> (NoMethodError)
From what I can see, operation is a valid object and should have this method. Am I missing something here?
Update 1
This is my updated code but when I run it the POST action does not seem to fire. Am I still missing any code? I do not get any errors either.
data = {token: "2xCGdzcuEeNzhs5tYaa8f", task: 1, message: "message", latitude: 1, longitude: 1}
client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/"))
request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data|
image_data = UIImagePNGRepresentation(image.image)
form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png')
})
operation = AFJSONRequestOperation.alloc.initWithRequest(request)
operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'},
failure: lambda { |operation, error| puts 'error' })
The request object outputs this:
<NSMutableURLRequest:195641792 - url: http://api.example.com/api/v1/messages,
headers: {"Content-Length"=>"118200", "Content-Type"=>"multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY", "Accept-Language"=>"en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8", "User-Agent"=>"theapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)"},
cache policy: 0, Pipelining: false, main doc url: , timeout: 60.0, network service type: 0 >
The operation object outputs this:
<AFJSONRequestOperation:0xa78c660>
The method signature is setCompletionBlockWithSuccess(lambda..., failure: lambda...)
-- you need to add a failure parameter. Something like this:
operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'},
failure: lambda { |operation, error| puts 'error' })
Also, you'll want to make sure to actually start the request operation:
operation.start
See https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFJSONRequestOperation.m#L102-L103 for the definition of this method.