gpt4 book ai didi

json - 使用自定义 JSON BODY 进行 HTTP post 的最佳方式是什么

转载 作者:行者123 更新时间:2023-11-30 10:12:48 25 4
gpt4 key购买 nike

我正在努力:(要使用自定义 JSON Body 发出 HTTP post 请求,我已经尝试使用以下代码使用 Alamofire :

let list = [[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]]
let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]
let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)
let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding)



Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({
(convertible, params) in
var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
return (mutableRequest, nil)
}))
.response { request, response, data, error in
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
println(dataString)
}

但这不起作用,我应该删除列表才能使其工作,为什么我的 JSON 正文中不能有列表

我的 JSON 正文如下所示:

{
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List":[
{
"IdQuestion" : 5,
"IdProposition": 2,
"Time" : 32
},
{
"IdQuestion" : 4,
"IdProposition": 3,
"Time" : 9
}
]
}

是否有任何库或您推荐的任何东西可以使此 HTTP post 请求正常工作?

最佳答案

效果很好

let list = [["Time": 30, "IdQuestion": 6510, "idProposition": 10], ["Time": 30, "IdQuestion": 8284, "idProposition": 10]]
let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]

let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)

let post = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String



httpPost("http://my1test.ru/stack/test.php", postData: "val="+post) {res,code in
println(res)
}

然后我编写了一个测试 php 文件来查看从应用程序获得的结果:

<?
$d = json_decode($_POST['val']);
echo $d->IdUser;
echo "\n".$d->List[0]->IdQuestion;
?>

我明白了

我用来发出 POST 请求的函数如下:

func httpPost(url:String, postData: String, completion: (String, Int) -> Void) {

var request = NSMutableURLRequest(URL: NSURL(string: url)!)

request.HTTPMethod = "POST"

request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding);

request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36", forHTTPHeaderField: "User-Agent")
request.addValue("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", forHTTPHeaderField: "Accept")
request.addValue("gzip, deflate, sdch", forHTTPHeaderField: "Accept-Encoding")
request.addValue("max-age=0", forHTTPHeaderField: "Cache-Control")

var session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(request, completionHandler: { data, response, error in
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode

completion(NSString(data: data, encoding: NSUTF8StringEncoding) as! String, statusCode)
}
})

task.resume()

}

关于json - 使用自定义 JSON BODY 进行 HTTP post 的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988835/

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