如何通过key => value发送值作为post方法swift
问题描述:
我想通过key=>value
发送一些参数,如volley
android(类似于form
html)的库。如何通过key => value发送值作为post方法swift
我dont't要发送的值作为JSON参数。
我该怎么办?
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("site", "code");
params.put("network", "tutsplus");
答
您应该使用Dictionary
和串行化处理的字典作为JSON并将其设置为HTTPBody
:
let parameters = ["site":"code", "network":"tutsplus"] as Dictionary<String, String>
let request = URLRequest(url: URL(string:YOURURL)!)
let session = URLSession.shared
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [])
另一种方法是:
request.httpBody = "site=code&network=tutsplus"
答
你可以只设置键和值在httpBody,一S,从而:
request.httpBody = "key=value"
你侃甚至通过添加“&”增加更多的按键,有点像你通常会在做GET
request.httpBody = "key=value&otherKey=otherValue"
请出示您试图代码 –
我是一个Android开发者我不知道如何发送像没有json值的参数值。在抽象的Map params = new HashMap (); //的POST参数: params.put( “点”, “代码”); params.put(“network”,“tutsplus”); ' –
可以使用AFNetworking,Alamofire或NSUrlsession它 –