我正在使用必需的 body 参数发出POST
请求,在POST
中测试此POSTMAN
请求可以给我正确的响应,但是当我传递相同的请求时我的代码中的body参数出现以下错误-:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.
代码-:
func submitStudentAttendance(url:String,id:String,SessionID: Int,SchoolID:Int,BatchID:Int,ClassID:Int,SectionID:Int,Medium:Int,Date:String,accessToken:String) {
let params : [String:Any] = [
"$id":"1",
"SchoolID":1,
"SessionID":42,
"BatchID":106,
"ClassID":634,
"SectionID":1246,
"Medium":"English",
"Date":"0001-01-01T00:00:00",
"CreatedBy":118,
"CreatedOn":"2019-04-14T17:02:34",
"studentList":"",
"AttendenceData":"[{\"ID\":0,\"SchoolID\":null,\"SessionID\":null,\"ClassID\":634,\"SectionID\":1246,\"StudID\":\"DSM00399\",\"SubjectID\":null,\"Date\":\"0001-01-01T00:00:00\",\"Attendence\":\"P\",\"CreatedOn\":null,\"CreatedBy\":0,\"IsActive\":null,\"Remarks\":\"rem16\",\"timeID\":null,\"AtndType\":null,\"BatchID\":null,\"Medium\":null}]"
]
let urlRequest = NSURL(string: url)
var request = URLRequest(url: urlRequest! as URL)
let tokenString = "Bearer " + accessToken
request.httpMethod = "POST"
request.setValue(tokenString, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let data = try! JSONSerialization.data(withJSONObject: params, options: [])
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = json!.data(using: String.Encoding.utf8.rawValue);
let alamoRequest = Alamofire.request(request as URLRequestConvertible)
alamoRequest.responseString {
response in
switch response.result {
case .success:
if response.data != nil {
do{
let json = try JSON(data: response.data!)
print(json)
}catch{
print(error)
}
}
break
case .failure(let error):
print(error)
}
}
}
正文部分与POSTMAN
中的正文部分完全相同,但仍然无法正常工作。谁能让我知道这里出了什么问题?这是服务器端问题,还是我以错误的格式发送了正文请求?
邮递员响应-:
答案 0 :(得分:1)
当您在问题中声明的参数转换为JSON时,看起来像这样。在这里,AttendanceData值不会转换为正确的JSON结构。它看起来像一个字符串。
您的参数中的JSON:
{
"Date": "0001-01-01T00:00:00",
"CreatedBy": 118,
"$id": "1",
"BatchID": 106,
"ClassID": 634,
"Medium": "English",
"studentList": "",
"SchoolID": 1,
"SectionID": 1246,
"AttendenceData": "[{\"ID\":0,\"SchoolID\":null,\"SessionID\":null,\"ClassID\":634,\"SectionID\":1246,\"StudID\":\"DSM00399\",\"SubjectID\":null,\"Date\":\"0001-01-01T00:00:00\",\"Attendence\":\"P\",\"CreatedOn\":null,\"CreatedBy\":0,\"IsActive\":null,\"Remarks\":\"rem16\",\"timeID\":null,\"AtndType\":null,\"BatchID\":null,\"Medium\":null}]",
"CreatedOn": "2019-04-14T17:02:34",
"SessionID": 42
}
但是,如果您按如下所示声明param并将其转换为JSON,则AttendanceData值将正确转换为JSON。
let params : [String:Any] = [
"$id":"1",
"SchoolID":1,
"SessionID":42,
"BatchID":106,
"ClassID":634,
"SectionID":1246,
"Medium":"English",
"Date":"0001-01-01T00:00:00",
"CreatedBy":118,
"CreatedOn":"2019-04-14T17:02:34",
"studentList":"",
"AttendenceData":[
"ID":0,"SchoolID":"null","SessionID":"null","ClassID":634,"SectionID":1246,"StudID":"DSM00399","SubjectID":"null","Date":"0001-01-01T00:00:00","Attendence":"P","CreatedOn":"null","CreatedBy":0,"IsActive":"null","Remarks":"rem16","timeID":"null","AtndType":"null","BatchID":"null","Medium":"null"
]
]
JSON
{
"$id": "1",
"BatchID": 106,
"CreatedBy": 118,
"ClassID": 634,
"studentList": "",
"SectionID": 1246,
"SessionID": 42,
"AttendenceData": {
"IsActive": "null",
"AtndType": "null",
"SectionID": 1246,
"timeID": "null",
"SessionID": "null",
"Attendence": "P",
"Date": "0001-01-01T00:00:00",
"Remarks": "rem16",
"Medium": "null",
"SchoolID": "null",
"BatchID": "null",
"ClassID": 634,
"StudID": "DSM00399",
"SubjectID": "null",
"CreatedBy": 0,
"ID": 0,
"CreatedOn": "null"
},
"Date": "0001-01-01T00:00:00",
"CreatedOn": "2019-04-14T17:02:34",
"Medium": "English",
"SchoolID": 1
}
希望它也可以解决您的问题。