Language: Swift
TargetOS: MacOS
I need to encode an image as a base64string and upload it as a PNG using IMGUR's api using an http post request. I have another function which takes a screenshot and creates it as a CGImage object. I used the provided base64string example from their api R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
which results in a successful 200 status code. Unfortunately replacing this string with my imageToBase64String() function (below) results in an error 415 status code. I am able to display the Base64 image string in HTML without any issues.
Function to convert CGImage screenshot into Base64String:
func imageToBase64String(_ cgImage: CGImage) -> String? {
let imageRep = NSBitmapImageRep(cgImage: cgImage).representation(using: NSBitmapImageRep.FileType.png, properties: [:])
let base64Image:String = ((imageRep!.base64EncodedString()))
Logger.write(base64Image)
return base64Image
}
Here is the anonymous upload for imgur:
func anonymousUpload(_ image: CGImage){
let base64Image = imageToBase64String(image)!
// Using this works fine
//let base64Image = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
let url = "https://api.imgur.com/3/upload"
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
request.httpMethod = "POST"
request.setValue("Client-ID \(imgurAPI.CLIENT_ID)", forHTTPHeaderField: "Authorization")
let bodyString = "image=\(base64Image)&type=base64&title=sample&description=Desktop screenshot"
request.httpBody = bodyString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if (error != nil){
print("error: \(error)")
return
}
print("response: \(response!)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("response string: \(responseString!)")
}
task.resume()
}
It looks like I constructed the body incorrectly. Below is the proper solution how to do so. I closely followed Postmans' generated http code to construct the request below:
func anonymousUpload(_ image: CGImage){
// Convert the file to base64
let base64Image:String = imageToBase64String(image)!
// Create our url
let url = URL(string: "https://api.imgur.com/3/image")!
let request = NSMutableURLRequest.init(url: url)
request.httpMethod = "POST"
request.addValue("Client-ID " + imgurAPI.CLIENT_ID, forHTTPHeaderField: "Authorization")
// Build our multiform and add our base64 image
let boundary = NSUUID().uuidString
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let body = NSMutableData()
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"image\"\r\n\r\n".data(using: .utf8)!)
body.append(base64Image.data(using: .utf8)!)
body.append("\r\n".data(using: .utf8)!)
body.append("--\(boundary)--\r\n".data(using: .utf8)!)
request.httpBody = body as Data
// Begin the session request
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if (error != nil){
print("error: \(error)")
return
}
print("response: \(response!)")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("response string: \(responseString!)")
}
task.resume()
}