【问题标题】:Swift http post has no post dataSwift http post 没有帖子数据
【发布时间】:2017-05-12 18:57:10
【问题描述】:

当尝试通过 http 向服务器发送 post 数据时,它不返回任何 post 数据。这是一个示例代码:

var request = URLRequest(url: URL(string: "http://posttestserver.com/post.php")!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let json = "{\"key\":\"c7cbdc09820372\",\"rand\": \"13baa5274c2b107727\"}"
request.httpBody = json.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
      if data != nil, let result = String(data: data!, encoding: .utf8) {
          print("\(result)")
      }
}.resume()

结果是:

成功转储 0 个帖子变量...没有帖子正文。

【问题讨论】:

  • 您尝试过使用 AlamoFire 吗?这会让这件事变得容易得多。
  • 谢谢,如果我找不到解决方案,我可以使用它。

标签: json swift http post urlsession


【解决方案1】:

将您的 json 字符串转换为字典,然后

jsonData = 试试? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

request.httpBody = jsonData

【讨论】:

  • 谢谢,但这无济于事
【解决方案2】:

此代码有效,以防万一有人需要:

var request = URLRequest(url: URL(string: "http://posttestserver.com/post.php")!)
request.httpMethod = "POST"
let data = "key=c7cbdc09820372&rand=13baa5274c2b107727"
request.httpBody = json.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
    if data != nil, let result = String(data: data!, encoding: .utf8) {
        print("\(result)")
    }
}.resume()

给予:

成功转储 2 个帖子变量....

【讨论】:

    【解决方案3】:

    我认为 http post/get 请求的最佳功能 参数 = “参数 1=值&参数 2=值 2” 如果你想使用 json 你应该更改为 Content-Type 值不要忘记

    public func HttpPost(params:String, completion: @escaping (_ success: Bool, _ object: NSDictionary?) -> ()) {
                let configuration = URLSessionConfiguration.default
                let session = URLSession(configuration: configuration)
                let url = NSURL(string: /* here post url */)
                var request = URLRequest(url: url! as URL)
                request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
                request.httpMethod = "POST"
                request.httpBody = params.data(using: String.Encoding.utf8)!
                let task = session.dataTask(with: request) {
                    data, response, error in
                    if let httpResponse = response as? HTTPURLResponse {
                        let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                        if json == nil {
                            completion(false, nil)
                        }
                        else{
                            completion(true, json as! NSDictionary?)
                        }
                    }
                    if (error != nil) {
                        completion(false, nil)
                    }
                }
                task.resume()
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多