Skip to Content
0
Feb 10, 2019 at 10:12 AM

HTTP 400 error during "Scene Text Detection" using iOS

111 Views

Hi Swift Guru's,

I have been trying to develop an iOS app to connect to sandbox of Leonardo Machine Learning Foundation. Objective is to take a picture using iPhone and "POST" the image to the sandbox server of Leonardo Foundation to get the results. I am using latest Xcode Version 10.1 (10B61).

I am able to access the same using cURL but with Swift code I am getting a HTTP response code of 400. I have tried to construct a "multipart/form-data" using whatever information I could get on the web but it has not been working for me. Please guide me if you have done anything similar before.

Attaching the piece of code where I am trying to connect with SAP

func connectWithSAP(photoURL : URL, photoData : String, sentImageData : Data){

if let myNewURL = URL(string: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition") {

var myRequest = URLRequest(url: myNewURL)

// Constructing the header

myRequest.addValue("multipart/form-data; --\(boundary)", forHTTPHeaderField: "Content-Type")

myRequest.addValue("xxxxxxxxxxxxx", forHTTPHeaderField: "APIKey")

myRequest.addValue("application/json", forHTTPHeaderField: "Accept")

myRequest.httpMethod = "POST"

myRequest.cachePolicy = .reloadIgnoringLocalCacheData

myRequest.timeoutInterval = 60.0


// Creating the format for HTTPbody

var data = Data()

var dataString = ""

dataString.append("--\(boundary)\r\n")

dataString.append(contentsOf: "Content-Disposition:form-data;name=\"files\"\r\n\r\n")

dataString.append("--\(boundary)\r\n")

dataString.append(contentsOf: "Content-Disposition:form-data;filename=\"\(photoURL)\" \r\n")

dataString.append("--\(boundary)\r\n")

dataString.append(contentsOf: "Content-Disposition:form-data;Content-Type:image/jpeg \r\n")

dataString.append("--\(boundary)\r\n")

data = dataString.data(using: .utf8)!

data.append(sentImageData)

data.append("--\(boundary)-----".data(using: .utf8)!)

myRequest.httpBody = data


// Calling the URL for connection

let task = URLSession.shared.dataTask(with: myRequest) { (data, response, error) in

if let error = error {

print(error)

}

// While assessing the httpResponse Code getting 400 as statusCode code. Details are mentioned below.

guard let httpResponse = response as? HTTPURLResponse,

(200...299).contains(httpResponse.statusCode) else {

print(error as Any)

return }

if let mimeType = httpResponse.mimeType,

mimeType == "application/json",

let data = data {

do {

let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]

// do something

// DispatchQueue.main.async {

// self.dismiss(animated: true, completion: nil)

// }

print(json as Any)

}catch {

print(error)

}

}

}

task.resume()

}

}

}

}

Getting error at the httpResponse part of the code 400, Printing out the description as

{ URL: https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition } { Status Code: 400, Headers {\n Connection = (\n \"keep-alive\"\n );\n \"Content-Length\" = (\n 131\n );\n \"Content-Type\" = (\n \"application/json\"\n );\n Date = (\n \"Sun, 10 Feb 2019 09:41:56 GMT\"\n );\n Server = (\n \"Werkzeug/0.14.1 Python/3.5.5\"\n );\n \"Strict-Transport-Security\" = (\n \"max-age=31536000; includeSubDomains; preload;\"\n );\n \"X-Vcap-Request-Id\" = (\n \"5f279f0a-56d7-48a8-76ce-58c9bd584cb0\"\n );\n} }"

Can you please help me correcting the detail which needs to be sent to the Sandbox sever ?

Regards

Gunjan