douyuan4357 2017-01-01 11:57
浏览 362
已采纳

在Alamofire请求的正文中发送JSON数据

I'm trying to make an api call to submit some data using Alamofire (version 4.0). I think the request has succeed. But the trouble i'm having is that when making the call I get a response from the server that 0 byte FAILURE.

I have tried many of the solutions currently on StackOverflow and cannot find a solution. Thanks for your help.

Here is my Postman setup:

enter image description here

My Swift code:

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let headers: HTTPHeaders = [ "content-type": "x-www-form-urlencoded"]


        Alamofire.request("myurl.php?method=GET_RECENT", headers: headers).responseJSON { response in
            debugPrint(response)
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
        }

    }
}

The error I'm getting is:

[Request]: myurl.php?method=GET_RECENT [Response]: { URL: myurl.php?method=GET_RECENT } { status code: 200, headers { "Cache-Control" = "max-age=0"; Connection = "Keep-Alive"; "Content-Length" = 0; "Content-Type" = "text/html; charset=UTF-8"; Date = "Sun, 01 Jan 2017 11:41:31 GMT"; Expires = "Sun, 01 Jan 2017 11:41:31 GMT"; "Keep-Alive" = "timeout=7, max=100"; Server = "Apache/2.2.15 (CentOS)"; "X-Powered-By" = "PHP/5.6.25"; } } [Data]: 0 bytes [Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) [Timeline]: Timeline: { "Request Start Time": 504963459.698, "Initial Response Time": 504963459.821, "Request Completed Time": 504963459.821, "Serialization Completed Time": 504963459.821, "Latency": 0.123 secs, "Request Duration": 0.123 secs, "Serialization Duration": 0.001 secs, "Total Duration": 0.124 secs } Optional(myurl.php?method=GET_RECENT) Optional( { URL: myurl.php?method=GET_RECENT } { status code: 200, headers { "Cache-Control" = "max-age=0"; Connection = "Keep-Alive"; "Content-Length" = 0; "Content-Type" = "text/html; charset=UTF-8"; Date = "Sun, 01 Jan 2017 11:41:31 GMT"; Expires = "Sun, 01 Jan 2017 11:41:31 GMT"; "Keep-Alive" = "timeout=7, max=100"; Server = "Apache/2.2.15 (CentOS)"; "X-Powered-By" = "PHP/5.6.25"; } }) Optional(0 bytes) FAILURE

  • 写回答

1条回答 默认 最新

  • dqbn76906 2017-01-01 14:17
    关注

    I think you are setting your Postman setup is invalid. To send JSON as data in your GET request your should be using raw options, like on the picture below.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?