我在将JSON数据转换为字符串方面存在问题。
var url = "http://10.1.10.98/POSSytem/api/inventories/PutInventory?received={'id':'coke','price':4.99,'In_stock':2}"
print(url)
Alamofire.request(str, method: .post, parameters: [:], encoding: JSONEncoding, headers: nil)
.response { (res) in
print(res)
}
日志: 原始字符串 -> http://10.1.10.98/possytem/api/inventories/putinventory?received= {'id'':'coke','price':4.99,'in_stock':2}
defaultDataResponse(请求:nil,响应:nil,数据:可选(0( 字节(,错误: 可选(alamofire.aferror.invalidurl(" http://10.1.10.98/possytem/possytem/api/inventories/putinventory?received== {'id ':'iD ':'coke 'co 'co 'co 'in_stock ':2}"(,(, 时间轴:时间轴:{"请求开始时间":514563518.605,"初始 响应时间:514563518.597,"请求完成的时间": 514563518.597,"序列化完成时间":514563518.605,"延迟":-0.007秒,"请求持续时间":-0.007 secs, "序列化持续时间":0.007秒,"总持续时间":0.000秒},, _metrics:nil(
由于某种原因,Alamofire将URL变成不同的URL。在JSON字符串中添加了逃脱的字符。Alamofire 4.4.0它应该一直留下来:" http://10.1.10.98/possytem/api/api/inventories/putinventory?received== {'
guard let js = try? JSONSerialization.data(withJSONObject: dict, options: []) else {
print("Error parsing json data (dict)")
return
}
let theJSONText = String(data: js,encoding: .ascii)
url += "(theJSONText!)"
let finalURL = url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
Alamofire.request(finalURL!, method: .post)
.response { (res) in
print(res)
}
使用
添加percencenCoding
它编码了有效URL的JSON字符串,并触发了发布呼叫。