[英]How to add JSON String to an HTTP request header?
This is my request function:这是我的请求 function:
static func request(url:String, method: HTTPMethod, parameters: [String:AnyObject], completion: @escaping (Optional<AnyObject>) -> (), errorBlock:@escaping (String, Int) -> (Bool)){
let url = URL(string: "\(kAPIHostname)\(url)")!
var req = URLRequest(url: url)
req.httpMethod = method.rawValue
req.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [])
APIManager.process(request: req, completion: completion, errorBlock: errorBlock)
}
What I'm trying to do is to add to the request header all of the parameters
instead of the body here.我想要做的是将所有
parameters
添加到请求 header 而不是此处的正文。
Is there a way to do this?有没有办法做到这一点?
So if parameters dictionary of headers, than add this code before or after req.httpBody =...因此,如果参数字典是标题,则在req.httpBody = ...之前或之后添加此代码
var headerParameters: [String: String] = [:]
for header in parameters {
headerParameters[header.key] = (header.value as? String ?? "")
}
req.allHTTPHeaderFields = headerParameters
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.