使用场景文本识别API连接SAP Leonardo沙箱html" target="_blank">服务器时出现HTTP 400错误
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)
myRequest.addValue("multipart/form-data; --\(boundary)", forHTTPHeaderField: "Content-Type")
myRequest.addValue("application/json", forHTTPHeaderField: "Accept")
myRequest.addValue("xxxxxxxxxxx", forHTTPHeaderField: "APIKey")
myRequest.httpMethod = "POST"
myRequest.cachePolicy = .reloadIgnoringLocalCacheData
myRequest.timeoutInterval = 60.0
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:image/jpeg \r\n\r\n")
dataString.append(photoData)
dataString.append("--\(boundary) ----- \r\n")
data = dataString.data(using: .utf8)!
myRequest.httpBody = data
let task = URLSession.shared.dataTask(with: myRequest) { (data, response, error) in
if let error = error {
print(error)
}
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]
print(json as Any)
}catch {
print(error)
}
}
}
task.resume()
}
我在响应对象中得到以下详细信息
{url:https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition/scene-text-recognition}{Status code:400,Headers{\n Connection=(\n\“keep-alive”\n);\n\“content-length”=(\n\“keep-alive”\n);\n\“content-length”=(\n131\n);\n\“content-type”=(\n\“application/json\”\n);36000;IncludeSubdomains;预加载;\“\n);\n\”x-vcap-request-id\“=(\n\”FEA7037C-4E48-49D2-4BE1-53B0DAD0EE46\“\n);\n}
正如您所看到的,状态代码是HTTP400。在从服务器获得正确的响应和数据方面需要一些帮助。
很可能尸体数据搞乱了。以下是工作代码:
let boundaryConstant = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
let headers = [
"APIKey": "YourAPIKEY"
]
let contentType = "multipart/form-data; boundary=" + boundaryConstant
//API endpoint for API sandbox
var request = URLRequest(url: URL(string: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition")!)
//setting request method
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let path1 = Bundle.main.path(forResource: "your_image", ofType: "png")!
let url = URL(fileURLWithPath: path1)
let fileName = url.lastPathComponent
let data = try? Data(contentsOf: url)
let imageData = UIImage.init(data: data!)!
let pngData = UIImagePNGRepresentation(imageData)!
let mimeType = "image/png"
let boundaryStart = "--\(boundaryConstant)\r\n"
let boundaryEnd = "--\(boundaryConstant)--\r\n"
let fieldName = "files"
let contentDispositionString = "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(fileName)\"\r\n"
let contentTypeString = "Content-Type: \(mimeType)\r\n\r\n"
var body = Data()
body.append(boundaryStart.data(using: .utf8)!)
body.append(contentDispositionString.data(using: .utf8)!)
body.append(contentTypeString.data(using: .utf8)!)
body.append(pngData)
body.append("\r\n".data(using: .utf8)!)
body.append(boundaryEnd.data(using: .utf8)!)
request.httpBody = body
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue(String(body.count), forHTTPHeaderField: "Content-Length")
let dataTask = session.dataTask(with: request) { (data, response, error) in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
}
dataTask.resume()
您也可以使用Alamofire上传图像。它更干净,不需要太多地玩弄“身体”:
let headers: HTTPHeaders = [
"APIKey": "<<Your API KEY>>",
"Content-type": "multipart/form-data"
]
let parameters:[String: String] = [:] //any other parameters you need to send
let path1 = Bundle.main.path(forResource: "<<your_image>>", ofType: "<<png or jpeg>>")!
let url = URL(fileURLWithPath: path1)
let fileName = url.lastPathComponent
let data = try? Data(contentsOf: url)
let imageData = UIImage.init(data: data!)!
//converting it into png data
let pngData = UIImagePNGRepresentation(imageData)
let mimeType = "image/png"
let fieldName = "files"
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = pngData{
multipartFormData.append(data, withName: fieldName, fileName: fileName, mimeType: mimeType)
}
}, usingThreshold: UInt64.init(), to: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition" , method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded")
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
问题内容: 没有与Postgres等效的东西。有没有人写过解码为函数? 问题答案: 有一个等效的。这称为声明。 CASE有两种形式: 简单案例: 搜索的案例: 陈述更容易阅读;我在Oracle中更喜欢这些。
我正在尝试编写非常有效的海明距离代码。受到Wojciech Muva极其巧妙的SSE3 popcount实现的启发,我编码了一个AVX2等效的解决方案,这次使用了256位寄存器。l期望至少有30%-40%的改进,基于所涉及的操作的双倍并行度,然而令我惊讶的是,AVX2代码慢了一点点(大约2%)! 两个64字节块的展开SSE3汉明距离: 使用AVX 256位寄存器的未展开等效版本: 我已经验证了编译
问题内容: 我正在从xml配置转移到注释。我想转换一个会话范围的bean是 可以通过注释完成此操作吗?如果没有,我该怎么做才能使该声明继续工作? 问题答案: 在spring上下文xml中,执行以下操作: 请注意,尽管如此,你将需要为该包中的所有类编写接口。
问题内容: 据我所知,Java没有C#之类的东西。是否有其他Java库提供类似功能?( 反射反射 )有什么区别? 问题答案: 除了达林的出色答案(+1)外,ASM也值得一试。
问题内容: 我试图找到与generate_series()(PostgreSQL语法)相当的snowflake。 问题答案: 这就是我能够在Snowflake中生成一系列日期的方式。我将行数设置为1095,以获得3年的日期,您当然可以将其更改为适合您的用例的日期 最初在这里找到 编辑:此解决方案是不正确的。不能保证序列无间隙。请遵循其他答案,而不是这个。感谢@Marcin Zukowski指出这一
标题说明了一切。我的问题是关于不同的字符串等价方法的效率。我经常使用<代码>。equalsIgnoreCase(String str),因为我对它很有兴趣。但我开始怀疑这是否是寻找字符串之间等价性的最有效方法。在我看来,正在调用一种大小写转换方法或,然后在其定义中调用,但我可能错了。那么,以下哪种方法在以下情况或任何情况下更有效?