我正在使用以下代码调用Azure Form Recognizer REST API,用于我的iOS Swift应用中的收据模型。这不会返回任何错误代码,但响应只是空的。我也在Post Man中尝试过,也得到了空响应。
如何从这个API调用中获取数据?
func recognizeReceipt(imageData: Data, apiKey: String, endpoint: String) {
let urlString = "\(endpoint)/formrecognizer/documentModels/prebuilt-receipt:analyze?api-version=2022-08-31"
guard let url = URL(string: urlString) else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue(apiKey, forHTTPHeaderField: "Ocp-Apim-Subscription-Key")
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
request.httpBody = imageData
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid response")
return
}
if (200...299).contains(httpResponse.statusCode) {
if let data = data {
// Process the response data
let resultString = String(data: data, encoding: .utf8)
print("Result: \(resultString ?? "")")
}
} else {
print("API request failed. Status code: \(httpResponse.statusCode)")
}
}
task.resume()
}
字符串
谢谢你
1条答案
按热度按时间gjmwrych1#
我发现,一旦你得到了成功响应,你必须再次调用Azure来获得
Operation Location
值的结果:字符串
然后使用以下方法提取数据:
型