-1

SWIFT代码:

func set(data:[AirFreightModel],userid:String,username:String,completion:@escaping(Result<String,CustomError>)->Void)
{
    let url = URL(string: "\(CS.ServerName)/AirFreight/SetAirFreightApproval?UserID=\(userid)&UserName=\(username)")!
    var request = URLRequest(url: url)
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"
    //request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    do{
        let jsonData = try! JSONEncoder().encode(data)
        let jsonString = String(data: jsonData, encoding: .utf8)!
        print(jsonString)

        let parameters: [String: String] = [
            "AirFreight": jsonString
        ]
        //request.httpBody = parameters.percentEncoded()
        request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions())
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data,
                let response = response as? HTTPURLResponse,
                error == nil else {                                              // check for fundamental networking error
                    print("error", error ?? "Unknown error")
                    return
            }

            guard (200 ... 299) ~= response.statusCode else {                    // check for http errors
                print("statusCode should be 2xx, but is \(response.statusCode)")
                print("response = \(response)")
                return
            }

            do{
                let decoder=JSONDecoder()
                let userResponse=try decoder.decode(String.self, from: data)
                completion(.success(userResponse))
            }catch
            {
                print("Unexpected  error: \(error)")
                completion(.failure(.canNotProcessData))
            }
        }
        task.resume()
    }
    catch
    {

    }

}

网页 API 代码:

public string SetAirFreightApproval(List<AirFreightModel> AirFreight,string UserID,String UserName)
{
return "1";
}

对 web api 的请求非常完美,因此 QueryString 变量数据传递工作正常,但对象数据数组无法传递。我是 Swift 的新手,所以不知道我在搞什么鬼。我正在使用 Swift 4.2。和 xcode 10. 示例 JSON 字符串:

[
  {
    "CauseID": " Matha Nosto Man",
    "CompanyName": "",
    "RaciActionList": [

    ],
    "PoType": "",
    "CauseDescription": "",
    "FBID": "Test",
    "ResponseResult": "",
    "RACI": "",
    "Destination": "",
    "Remarks": "",
    "Sequence": "",
    "Type": "",
    "BillDate": "",
    "FreightType": "",
    "BillAmount": "",
    "BillRef": "",
    "CommandName": "",
    "IsChecked": false,
    "EmployeeCode": "",
    "BuyerORExporter": "",
    "Mode": "",
    "Forwarder": "",
    "RunningFor": ""
  }
]

TIA
Web API 请求

哪里显示了对 web api 的请求已经完成,因为其余参数获取所有值都接受列表之一。

4

1 回答 1

0

如果 Web API 期望正文中包含 JSON(如我所料),那么准备请求应该如下所示(直接在此处键入,因此可能是拼写错误)

let parameters = [
    "AirFreight": data
]
// options by default [], so can be dropped
request.httpBody = try JSONSerialization.data(withJSONObject: parameters) 
// your can print debug of request.httpBody after serialisation to check

这将发送完整的 JSON 对象,

{
   "AirFreight": [
     {
       "CauseID": " Matha Nosto Man",
       "CompanyName": "",
       "RaciActionList": [

        ],
        "PoType": "",
        ...
   ]
}

与提供生成一个项目 JSON 的代码快照相反,例如 (scratchy)

{
   "AirFreight": "Very long flat string containing all objects properties"
}
于 2020-01-02T18:06:13.820 回答