0

Hey I'm trying to use the Mashape api CamFind to upload an image request but I'm not sure how to do it, Camfind want: - a multiform encoded request - the header to contain the key - paramaters: image_request[locale] and image_request[image]

I've been stuck on this for a very long time, any help is appreciated, I get a 400 error

Here's my code

 func post(){
    var TWITTERFON_FORM_BOUNDARY:String = "AaB03x"
    let url = NSURL(string: "https://camfind.p.mashape.com/image_requests")!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 10)
    var MPboundary:String = "--\(TWITTERFON_FORM_BOUNDARY)"
    var endMPboundary:String = "\(MPboundary)--"
    var data:NSData = UIImagePNGRepresentation(imageView.image)
    var filename:String = "image request image"
    var body:NSMutableString = NSMutableString();
    // params

    let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! NSString
    let documentDirectoryPath:NSString = path.stringByAppendingString("items.db")

    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
    let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        let imageURL = paths[0].stringByAppendingString("image.png")
    UIImagePNGRepresentation(imageView.image).writeToFile(imageURL, atomically: true)





    let parameters:NSDictionary = ["focus[x]": "480", "focus[y]": "640","image_request[language]": "en","image_request[locale]": "en_US","image_request[image]": imageURL]

        for (key, value) in parameters {
            body.appendFormat("\(MPboundary)\r\n")
            body.appendFormat("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendFormat("\(value)\r\n")
        }
    let parametersNS : NSData = NSKeyedArchiver.archivedDataWithRootObject(parameters)
    // image upload
    body.appendFormat("%@\r\n",MPboundary)
    body.appendFormat("Content-Disposition: form-data; name=\"\(filename)\"; filename=\"pen111.png\"\r\n")
    body.appendFormat("Content-Type: image/png\r\n\r\n")
    var end:String = "\r\n\(endMPboundary)"
    var myRequestData:NSMutableData = NSMutableData();
    myRequestData.appendData(body.dataUsingEncoding(NSUTF8StringEncoding)!)

    myRequestData.appendData(parametersNS)

    myRequestData.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!)
    var content:String = "multipart/form-data; boundary=\(TWITTERFON_FORM_BOUNDARY)"
    request.setValue(content, forHTTPHeaderField: "Content-Type")
    request.setValue("\(myRequestData.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = myRequestData
    request.HTTPMethod = "POST"
    request.setValue("9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", forHTTPHeaderField: "X-Mashape-Key")
    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            println(error)
        } else {
            let httpResponse = response as? NSHTTPURLResponse
            let statusCode = (response as? NSHTTPURLResponse)?.statusCode ?? -1
            let statusDescripton = httpResponse?.description

            println(httpResponse)


        }
    })

    dataTask.resume()


}
CDub
  • 13,146
  • 4
  • 51
  • 68
Kashish Goel
  • 94
  • 1
  • 9

1 Answers1

1

I'd suggest going through CloudSight directly instead of using Mashape. There's also an Objective C Cocoapod which you can use directly or learn from.

CDub
  • 13,146
  • 4
  • 51
  • 68
  • I'm trying to stick to swift since I'm not good with objective-c although I managed to get it working through mashape in objective c, but is there a way i can use the objective c code in a swift project? The only problem I'm facing is that I cant hook up the storyboard with the objective c file in my swift project, and I need to be able to hook up the image view. I was thinking about using the objc part for the POST and GET request and swift to build the app, do you know a fix for this? – Kashish Goel Aug 10 '15 at 17:23
  • Here's a good post on how to set up Cocoapods with a swift project: http://www.raywenderlich.com/97014/use-cocoapods-with-swift – Brad Folkens Aug 10 '15 at 18:05
  • As far as the imageView is concerned, it's easy: Make sure to hook up the imageView to the controller (if you're using interface builder), and then when the user presses some sort of action button, submit the imageView.image property to the image request method in the pod. – Brad Folkens Aug 10 '15 at 18:08