Json 如何使用Alamofire和swift 4为Clarifai API编写POST请求?

Json 如何使用Alamofire和swift 4为Clarifai API编写POST请求?,json,swift,post,alamofire,clarifai,Json,Swift,Post,Alamofire,Clarifai,我对Swift中的网络比较陌生,尤其是POST请求。我已经阅读了Clarifai API和Alamofire的文档,但还没有完全了解如何使用Alamofire编写工作请求。到目前为止,我已经找到了以下代码,它创建了一个符合Clarifai API的结构,该API希望请求的结构如下所示: curl -X POST \ -H "Authorization: Key YOUR_API_KEY" \ -H "Content-Type: application/json

我对Swift中的网络比较陌生,尤其是POST请求。我已经阅读了Clarifai API和Alamofire的文档,但还没有完全了解如何使用Alamofire编写工作请求。到目前为止,我已经找到了以下代码,它创建了一个符合Clarifai API的结构,该API希望请求的结构如下所示:

curl -X POST \
  -H "Authorization: Key YOUR_API_KEY" \
 -H "Content-Type: application/json" \
-d @https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs << FILEIN
 {
   "inputs": [
    {
    "data": {
      "image": {
        "base64": "$(base64 /home/user/image.png)"
      }
    }
  }
]
}
FILEIN
现在我不知道在这一点之后我应该做什么。我试着用POST写一个Alamofire请求,但被参数卡住了?可加性呢?对于参数,我尝试了以下方法:

let parameters: Parameters = [
        "Authorization: Key":"xxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type":"application/json"]

但我不知道这是否正确。如果有人能在这里帮助我,我将不胜感激!祝大家今天愉快

看起来您已经从对象和头部构造了jsonString(您将它们称为“参数”)。您应该能够通过以下方式发送:

let headers: [String: AnyObject] = [
    "Authorization: Key":"xxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type":"application/json"
]

Alamofire.request(.POST, "https://api.clarifai.com/v2/inputs", parameters: jsonString, headers: headers, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }

我将它们命名为标题,以使上面的内容更清楚。请注意,我已经屏蔽了授权密钥(无论是在回答中还是在原始问题中)-通常情况下,您都不想将其公开。

Hi!我也在尝试用Alamofire设置clarifai的RESTAPI,但似乎做不好。你找到解决办法了吗?谢谢没有,我还没发现……很有趣。当我尝试运行代码时,收到无效或格式错误的请求错误。是的,我收到了相同的错误。您能提供您看到的错误吗?alamofire请求是什么样子的(如果可能的话,代码就是这样的)。很难知道这里会出什么问题。
let headers: [String: AnyObject] = [
    "Authorization: Key":"xxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type":"application/json"
]

Alamofire.request(.POST, "https://api.clarifai.com/v2/inputs", parameters: jsonString, headers: headers, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }