• <bdo id='u02NU'></bdo><ul id='u02NU'></ul>

    <small id='u02NU'></small><noframes id='u02NU'>

      <i id='u02NU'><tr id='u02NU'><dt id='u02NU'><q id='u02NU'><span id='u02NU'><b id='u02NU'><form id='u02NU'><ins id='u02NU'></ins><ul id='u02NU'></ul><sub id='u02NU'></sub></form><legend id='u02NU'></legend><bdo id='u02NU'><pre id='u02NU'><center id='u02NU'></center></pre></bdo></b><th id='u02NU'></th></span></q></dt></tr></i><div id='u02NU'><tfoot id='u02NU'></tfoot><dl id='u02NU'><fieldset id='u02NU'></fieldset></dl></div>

      1. <legend id='u02NU'><style id='u02NU'><dir id='u02NU'><q id='u02NU'></q></dir></style></legend>
      2. <tfoot id='u02NU'></tfoot>

        HTTP Post 参数通过 json 编码到 $_POST

        HTTP Post Parameters are passed json encoded to $_POST(HTTP Post 参数通过 json 编码到 $_POST)

        <small id='c2G3y'></small><noframes id='c2G3y'>

        1. <legend id='c2G3y'><style id='c2G3y'><dir id='c2G3y'><q id='c2G3y'></q></dir></style></legend>
              • <bdo id='c2G3y'></bdo><ul id='c2G3y'></ul>
                <tfoot id='c2G3y'></tfoot>

                  <i id='c2G3y'><tr id='c2G3y'><dt id='c2G3y'><q id='c2G3y'><span id='c2G3y'><b id='c2G3y'><form id='c2G3y'><ins id='c2G3y'></ins><ul id='c2G3y'></ul><sub id='c2G3y'></sub></form><legend id='c2G3y'></legend><bdo id='c2G3y'><pre id='c2G3y'><center id='c2G3y'></center></pre></bdo></b><th id='c2G3y'></th></span></q></dt></tr></i><div id='c2G3y'><tfoot id='c2G3y'></tfoot><dl id='c2G3y'><fieldset id='c2G3y'></fieldset></dl></div>
                    <tbody id='c2G3y'></tbody>
                  本文介绍了HTTP Post 参数通过 json 编码到 $_POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不知道如何正确发送 POST 参数.

                  I can't figure out how to properly send POST parameters.

                  我的 Swift 3:

                  My Swift 3:

                  let parameters = ["name": "thom", "password": "12345"] as Dictionary<String, String>
                  let url = URL(string: "https://mywebsite.com/test.php")!
                  let session = URLSession.shared
                  var request = URLRequest(url: url)
                  request.httpMethod = "POST"
                  do
                  {
                      request.httpBody = try JSONSerialization.data(withJSONObject: parameters)
                  }
                  catch let error
                  {
                      print(error.localizedDescription)
                  }
                  request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
                  request.addValue("application/json", forHTTPHeaderField: "Accept")
                  let task = session.dataTask(with: request as URLRequest, completionHandler: 
                  {
                      data, response, error in
                      guard error == nil else
                      {
                          print(error as Any)
                          return
                      }           
                      guard let data = data else
                      {
                          return
                      }
                      do 
                      {
                          if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] 
                          {
                              print(json)
                              print(json["post"]!)
                          }
                          else
                          {
                              print("no json")
                          }
                      }
                      catch let error
                      {
                          print(error.localizedDescription)
                      }
                  })
                  task.resume()
                  

                  我的 PHP:

                  <?php
                  header('Content-Type: application/json');
                  if(empty($_POST)) echo json_encode(array('post'=>'empty'));
                  else echo json_encode($_POST+array('post'=>'not_empty'));
                  exit;
                  

                  如果我将内容类型标头(在 Swift 中)设置为 application/json 我得到:

                  If I set the content-type header (in Swift) to application/json I get:

                  ["post": empty]
                  empty
                  

                  如果我将它设置为 application/x-www-form-urlencoded 我得到:

                  If I set it to application/x-www-form-urlencoded I get:

                  ["{"name":"thom","password":"12345"}": , "post": not_empty]
                  not_empty
                  

                  如何将字典作为 $_POST 键/值对而不是 json_encoded 字符串发送到我的服务器?

                  How do I send the dictionary to my server as $_POST key/value pairs, not as a json_encoded string?

                  推荐答案

                  您想将请求百分比转义为 x-www-form-urlencoded 请求,如下所示:

                  You want to percent-escape the request into a x-www-form-urlencoded request, like so:

                  let parameters = ["name": "thom", "password": "12345"]
                  let url = URL(string: "https://mywebsite.com/test.php")!
                  
                  var request = URLRequest(url: url)
                  request.httpMethod = "POST"
                  request.updateHttpBody(with: parameters)
                  request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
                  
                  let task = session.dataTask(with: request) { data, response, error in
                      guard let data = data, error == nil else {
                          print("(error)")
                          return
                      }
                  
                      // handle response here
                  }
                  task.resume()
                  

                  哪里

                  extension URLRequest {
                  
                      /// Populate the `httpBody` of `application/x-www-form-urlencoded` request.
                      ///
                      /// - parameter parameters:   A dictionary of keys and values to be added to the request
                  
                      mutating func updateHttpBody(with parameters: [String : String]) {
                          let parameterArray = parameters.map { (key, value) -> String in
                              return "(key.addingPercentEncodingForQueryValue()!)=(value.addingPercentEncodingForQueryValue()!)"
                          }
                          httpBody = parameterArray.joined(separator: "&").data(using: .utf8)
                      }
                  }
                  
                  extension String {
                  
                      /// Percent escape value to be added to a HTTP request
                      ///
                      /// This percent-escapes all characters besides the alphanumeric character set and "-", ".", "_", and "*".
                      /// This will also replace spaces with the "+" character as outlined in the application/x-www-form-urlencoded spec:
                      ///
                      /// http://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-encoding-algorithm
                      ///
                      /// - returns: Return percent escaped string.
                  
                      func addingPercentEncodingForQueryValue() -> String? {
                          let generalDelimitersToEncode = ":#[]@?/"
                          let subDelimitersToEncode = "!$&'()*+,;="
                  
                          var allowed = CharacterSet.urlQueryAllowed
                          allowed.remove(charactersIn: "(generalDelimitersToEncode)(subDelimitersToEncode)")
                  
                          return addingPercentEncoding(withAllowedCharacters: allowed)?.replacingOccurrences(of: " ", with: "+")
                      }
                  }
                  

                  这篇关于HTTP Post 参数通过 json 编码到 $_POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='NI3sE'></bdo><ul id='NI3sE'></ul>
                    <i id='NI3sE'><tr id='NI3sE'><dt id='NI3sE'><q id='NI3sE'><span id='NI3sE'><b id='NI3sE'><form id='NI3sE'><ins id='NI3sE'></ins><ul id='NI3sE'></ul><sub id='NI3sE'></sub></form><legend id='NI3sE'></legend><bdo id='NI3sE'><pre id='NI3sE'><center id='NI3sE'></center></pre></bdo></b><th id='NI3sE'></th></span></q></dt></tr></i><div id='NI3sE'><tfoot id='NI3sE'></tfoot><dl id='NI3sE'><fieldset id='NI3sE'></fieldset></dl></div>
                    <tfoot id='NI3sE'></tfoot>

                  • <small id='NI3sE'></small><noframes id='NI3sE'>

                              <tbody id='NI3sE'></tbody>
                          1. <legend id='NI3sE'><style id='NI3sE'><dir id='NI3sE'><q id='NI3sE'></q></dir></style></legend>