gpt4 book ai didi

php - HTTP Post 参数以 json 编码传递给 $_POST

转载 作者:行者123 更新时间:2023-12-01 19:33:55 25 4
gpt4 key购买 nike

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

我的 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;

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

["post": empty]
empty

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

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

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

最佳答案

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

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: "+")
}
}

关于php - HTTP Post 参数以 json 编码传递给 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com