【问题标题】:How to pass HTTPBody in urlrequest swift如何在 urlrequest swift 中传递 HTTPBody
【发布时间】:2020-03-24 08:28:14
【问题描述】:

如何在 urlrequest 中传递 HTTPBody 这样的内容类型

下面是我从邮递员代码中尝试的代码-

  var semaphore = DispatchSemaphore (value: 0)

    let parameters = "code=httagtsgdgtdfsg&client_secret=agsfstf&Grant_type=authorization_code"
    let postData =  parameters.data(using: .utf8)
    var request = URLRequest(url: URL(string: "url")!,timeoutInterval: Double.infinity)
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "HTTPMethod"
    request.httpBody = postData
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
      guard let data = data else {
        print(String(describing: error))
        return
      }
      print(String(data: data, encoding: .utf8)!)
      semaphore.signal()
    }
    task.resume()
    semaphore.wait()

【问题讨论】:

  • 请 POSTMAN(右侧)生成 Swift 代码。
  • 我用过但收到错误请求(400),所以
  • 生成的代码是什么?
  • 邮递员添加的代码
  • 如果你愿意,你可以使用 swift 包。 github.com/bibinjacobpulickal/ServiceManager

标签: ios swift nsurlrequest


【解决方案1】:

这是什么?

request.httpMethod = "HTTPMethod"

应该是

request.httpMethod = "POST"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多