我试图使用alamofire库,但我有一个问题,我试图将下一个jsonString添加到请求的正文,它总是给我相同的400状态代码,有人可以帮助我,这是字符串和我的代码:
Json String:{"时刻":{"名称" :" hola","开始" :" 12-5-2005 0:0"," end" :" 22-4-2007 0:0","存储" :false}}
这是我的实际代码,谢谢
let jsonString = "{\"moment\":{" + "\"name\" : " + "\"" + nombre + "\"" + ",\"begin\" : \"" + fechaIni + "\"," +
"\"end\" : " + "\"" + fechaFin + "\"" + "," + "\"stored\" : false" + "}" + "}"
let request = NSMutableURLRequest(URL: NSURL(string: "http://whatmatters.deploy-cd.com/api/me/moments.json")!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "ContentType")
request.HTTPBody = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
Alamofire.request(request).authenticate(user: user, password: pass)
.response { resp in
print(resp)
}
答案 0 :(得分:0)
<强>更新强> 你必须在json中序列化你的数据
这里应该是什么样子:
let jsonString = "{\"moment\":{" + "\"name\" : " + "\"" + nombre + "\"" + ",\"begin\" : \"" + fechaIni + "\"," +
"\"end\" : " + "\"" + fechaFin + "\"" + "," + "\"stored\" : false" + "}" + "}"
let request = NSMutableURLRequest(URL: NSURL(string: "http://whatmatters.deploy-cd.com/api/me/moments.json")!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "ContentType")
let stringData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
do {
let jsonData:NSData? = try NSJSONSerialization.JSONObjectWithData(stringData!, options: .AllowFragments) as? NSData
request.HTTPBody = jsonData
} catch let caught as NSError {
print(caught)
}
Alamofire.request(request).authenticate(user: user, password: pass)
.response { resp in
print(resp)
}
答案 1 :(得分:0)
我这样做以发送json Post参数的方法是将参数放在Dictionary
中,如下所示:
let parameters = [
"moment" : [
"name" : nombre,
"begin" : fechaIni,
"end" : fechaFin,
"stored" : false,
]]
所以所有的代码都是这样的
var request = NSMutableURLRequest(URL: NSURL(string: "http://whatmatters.deploy-cd.com/api/me/moments.json")!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "ContentType")
// request.HTTPBody = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let parameters = [
"moment" : [
"name" : nombre,
"begin" : fechaIni,
"end" : fechaFin,
"stored" : false,
]]
request = Alamofire.ParameterEncoding.JSON.encode(request, parameters: parameters).0
Alamofire.request(request).authenticate(user: user, password: pass)
.response { resp in
print(resp)
}
注意:请注意字典值数据类型和json中所需的数据类型,即如果您需要所有json值a String
,请将字典值转换为{ {1}}
答案 2 :(得分:0)
如果您使用字典而不是字符串,可以在Alamofire文档中找到简短的解决方案:
https://github.com/Alamofire/Alamofire#post-request-with-json-encoded-parameters
public static void NewRecords()
{
using (var stageContext = new StagingTableDataContext())
{
using (var destinationContext = new DestinationTableDataContext())
{
var allProjectNames = destinationContext.THEOPTIONs.Select(u => u.NAME).ToList();
var deltaList = stageContext.ProjectMasters.ToList().Where(u => !allProjectNames.Contains(u.Finance_Project_Number)).ToList();
deltaList.ForEach(u => u.Processing_Result = 0);
deltaList.ForEach(u => u.Processing_Result_Text = "UNIQUE");
}
stageContext.SubmitChanges();
}
}