cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP 400 error during "Scene Text Detection" using iOS

gunjan_kumar7
Discoverer
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

gunjan_kumar7
Discoverer
0 Kudos

Thanks Robin for your reply. The file parameter has been maintained as String only as we have managed to include quotes here. Have written the body portion again. But it is still giving the same message with status code as 400.

Is it possible to get a detailed documentation of the API which describes each of the request parameters. In absence of this, it becoming difficult to assume values. Would request you if you may please help me with it.

// Reference of the body text

var data = Data()

var dataString = ""

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

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

dataString.append(contentsOf: "Content-Type:application/octet-stream \r\n\r\n")

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

data.append(sentImageData)

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

myRequest.httpBody = data

Regards

Gunjan

Qualiture
Active Contributor
0 Kudos

Hi Gunjan,

If you look at the Swift Code Snippet at https://api.sap.com/api/scene_text_recognition_api/resource the 'filename' parameter is of type String, not URL... maybe that's what causing the Bad Request error?

Also, they do some strange string concatenation for constructing the body, maybe the resulting body differs as well?

Hope this helps!