请求错误Google Cloud NLP API与Swift
我试图向Google Cloud NLP API发出请求,以获取一段文本的情感分析。我使用Postman来设计正确的请求,并且我能够使用Postman得到有效的响应。然而,当我试图从Swift发出相同的请求时,它给了我一个错误。用于发出请求的错误和代码片段如下所示请求错误Google Cloud NLP API与Swift,swift,google-cloud-platform,alamofire,sentiment-analysis,google-natural-language,Swift,Google Cloud Platform,Alamofire,Sentiment Analysis,Google Natural Language,我试图向Google Cloud NLP API发出请求,以获取一段文本的情感分析。我使用Postman来设计正确的请求,并且我能够使用Postman得到有效的响应。然而,当我试图从Swift发出相同的请求时,它给了我一个错误。用于发出请求的错误和代码片段如下所示 func sendAPIRequest(with text: String){ print("Text: ", text) let jsonRequest = [ [ "docume
func sendAPIRequest(with text: String){
print("Text: ", text)
let jsonRequest = [
[
"document":[
"type":"PLAIN_TEXT",
"language": "EN",
"content":"'Lawrence of Arabia' is a highly rated film biography about British Lieutenant T. E. Lawrence. Peter O'Toole plays Lawrence in the film."
],
"encodingType":"UTF8"
]
]
let jsonObject = JSON(jsonRequest)
let headers: HTTPHeaders = [
"X-Ios-Bundle-Identifier": "\(Bundle.main.bundleIdentifier ?? "") ",
"Content-Type": "application/json"
]
let APIRequest = Alamofire.request("https://language.googleapis.com/v1/documents:analyzeSentiment?key=\(gCloudAPIKey)", method: .post , parameters: jsonRequest as? [String: Any], encoding: JSONEncoding.default , headers: headers).responseJSON { (response) in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
错误:
JSON: {
error = {
code = 400;
details = (
{
"@type" = "type.googleapis.com/google.rpc.BadRequest";
fieldViolations = (
{
description = "Must have some text content to annotate.";
field = "document.content";
}
);
}
);
message = "One of content, or gcs_content_uri must be set.";
status = "INVALID_ARGUMENT";
};
}
对不起。解决了。根据Alamofire,我的jsonRequest应该是类型参数
let jsonRequest: Parameters =
[
"document":[
"type":"PLAIN_TEXT",
"language": "EN",
"content":"\(text)"
],
"encodingType":"UTF8"
]
对不起。解决了。根据Alamofire,我的jsonRequest应该是类型参数
let jsonRequest: Parameters =
[
"document":[
"type":"PLAIN_TEXT",
"language": "EN",
"content":"\(text)"
],
"encodingType":"UTF8"
]