【问题标题】:im fairly new and facing a problem about sending api request我很新,面临发送 api 请求的问题
【发布时间】:2020-03-15 22:01:34
【问题描述】:

我对 swift 还很陌生,我遇到了 Alamofire 的问题,我正在尝试发送一个带有 json 正文的 get 请求,但我的代码被困在这里:

    struct request {
    func getMessage(message:String){
    let parameters: [String:Any] =  [
        "message" : [
            "text": message,
            "type":"text"
        ],
        "type":"message",
        "recipentId":"",
        "info": [
            "token":""
        ]
    ]
        makeRequest(parameters: parameters)

}
    func makeRequest(parameters:[String:Any]){
        AF.request("https://bot.ortus4c.com/api/botController/token/5cc1f42e66d0ba0001dbf944",method: .post,parameters: parameters).response{
            response in
            debugPrint(response)
        }
    }

执行此代码后,我得到:

 [Request]: POST https://bot.ortus4c.com/api/botController/token/5cc1f42e66d0ba0001dbf944
[Request Body]: 
info%5Btoken%5D=&message%5Btype%5D=text&message%5Btext%5D=Karakter&recipentId=&type=message
[Response]: 
[Status Code]: 415
[Headers]:
Accept: application/json
Content-Length: 0
Date: Sat, 07 Mar 2020 19:10:23 GMT
Server: cloudflare
cf-cache-status: DYNAMIC
cf-ray: 57069dc3eaccad06-OTP
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
x-b3-traceid: b10ba988c0249901
[Response Body]: 
None
[Data]: None
[Network Duration]: 0.3303520679473877s
[Serialization Duration]: 0.0s
[Result]: success(nil)

与我通过邮递员发送此请求时的预期结果相比,我没有得到任何结果正文:

"result": {
        "messages": [
            {
                "type": "text",
                "text": "Karakter Analizine Hoşgeldiniz. Analize başlamak için \"karakter\" yazınız"
            },
            {
                "type": "text",
                "text": "Test"
            }
        ],
        "tag": [
            "start",
            "finish"
        ],
        "conversation_id": "5e63f302dc4bec0001883f01",
        "intent_name": "Default Karakter",
        "intent_id": "5ccb46ef66d0ba0001dbfcdf",
        "intent_score": null,
        "current_flow_id": "ee52db21-dae1-4587-80cd-7649798dddce1583608578222"
    }

那么我怎样才能用下面这个给定的正文发送这个帖子请求:

let parameters: [String:Any] =  [
            "message" : [
                "text": message,
                "type":"text"
            ],
            "type":"message",
            "recipentId":"",
            "info": [
                "token":""
            ]
        ]

现在我已经发送了请求并得到了一个如下所示的正文:

"result": {
        "messages": [
            {
                "extra": {},
                "question": "Adınız ?",
                "type": "text",
                "values": [
                    {
                        "text": ""
                    }
                ],
                "askCount": 1,
                "text": "Adınız ?",
                "waitUserInput": true
            }
        ],
        "tag": [
            "start",
            "p_firstName",
            1
        ],
        "conversation_id": "5e63ffa0dc4bec0001883f10",
        "intent_name": "Karakter Analizi",
        "intent_id": "5cc20ae266d0ba0001dbf946",
        "intent_score": "62.0",
        "current_flow_id": "62ac3ac8-0cd7-43e5-b92b-f119d49c40d41583611808850"
    }

如何将问题变量打印到屏幕上的标签上?

【问题讨论】:

    标签: swift api post


    【解决方案1】:

    响应代码 415 表示 Unsupported Media Type 并且响应的 Accept 标头让您知道服务器希望数据以 JSON 发送。您所要做的就是让 Alamofire 知道它应该在请求正文中将参数编码为JSON

    AF.request("https://bot.ortus4c.com/api/botController/token/5cc1f42e66d0ba0001dbf944",
               method: .post,
               parameters: parameters, 
               encoding: JSONEncoding.default).response { response in
        debugPrint(response)
    }
    

    【讨论】:

    • 我完成了它并得到了一个代码 500 我猜这意味着服务器错误这是否意味着我做错了什么或 api 只是没有响应?
    • 不,代码 500 始终是服务器端错误。您应该联系负责您使用的 API 的人员。
    • 你能回答我如何将问题参数打印到标签吗?
    【解决方案2】:

    您还需要在发送请求时提及编码类型。因为您的参数数据是 json,所以将编码参数添加为 encoding: JSONEncoding.default

    例如我曾用作:

    manager.request(url, method: httpMethod,
                            parameters: parameters,
                            encoding: JSONEncoding.default,
                            headers: configuredHeader)
                .validate(statusCode: 200..<300)
                .responseJSON(completionHandler: { [weak self] response in
                    self?.handleNetworkResponse(response: response, networkCompletionHandler: networkCompletionHandler)
                })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2022-11-10
      • 2018-09-13
      相关资源
      最近更新 更多