Ios Alamofire不允许直接发送对象

Ios Alamofire不允许直接发送对象,ios,swift,alamofire,Ios,Swift,Alamofire,我的API只接受对象作为主体,但alamofire只将Dictionary作为对象发送,我的服务器不接受请求帮助 我必须调用一个API,它是使用alamofire的PostAPI 只要我将模型转换为dictionary并将dictionary转换为json 发布它Alamofire不允许我发布字符串 它允许我发送api不接受的词典 ["key":"value"]- Not acceptable {"key":"value"}- Acceptable 有人能分享任何解决方案吗 我使用的是Swif

我的API只接受对象作为主体,但alamofire只将Dictionary作为对象发送,我的服务器不接受请求帮助

我必须调用一个API,它是使用alamofire的PostAPI

只要我将模型转换为dictionary并将dictionary转换为json 发布它Alamofire不允许我发布字符串

它允许我发送api不接受的词典

["key":"value"]- Not acceptable
{"key":"value"}- Acceptable
有人能分享任何解决方案吗

我使用的是Swift 5,Xcode 10,Alamofire 4.8.2

do{
    let d  = try data.asDictionary()
    jsonString = DictionaryToJSON(data: dictionary)
} catch {
    print(error)
}
Alamofire.request(url, method: .post, parameters: jsonString, encoding: .utf8, headers: [: ]).responseJSON { (res) in
    print(res.result)
    print("Request Data \(res.request) \n Dictionary \(jsonString)")
    do {
        let d = try JSONDecoder().decode([OTPMessage].self, from: res.data!)
        print(d[0].message)
    } catch {
        print(error)
    }
}

// Dictionary to JSON

func DictionaryToJSON(data: [String:Any])->String {
    if let theJSONData = try? JSONSerialization.data(
        withJSONObject: data,
        options: .prettyPrinted
    ),
    let theJSONText = String(data: theJSONData, encoding: String.Encoding.ascii) {
        print("JSON string = \n\(theJSONText)")
        return theJSONText
    }
    else {
        return ""
    }
}

// Object to Dictionary

extension Encodable {
    func asDictionary() throws -> [String: Any] {
        let data = try JSONEncoder().encode(self)
        guard let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
            throw NSError()
        }
        return dictionary
    }
}

//Struct
struct OTPMessage:Codable {
    var message = String()
}

有了阿拉莫菲尔,你无法做到这一点。您需要做的是创建一个
URLRequest
对象,设置它的
httpBody
属性,然后将其传递给Alamofire

URLRequest
允许您将
数据
作为帖子正文

            var request = URLRequest(url: urlFinal)
            request.httpMethod = HTTPMethod.post.rawValue
            request.allHTTPHeaderFields = dictHeader
            request.timeoutInterval = 10
            request.httpBody = newPassword.data(using: String.Encoding.utf8)

            Alamofire.request(request).responseString { (response) in
                if response.response!.statusCode >= 200 && response.response!.statusCode <= 300 {
                    completion("success")
                }else {
                    completion("failed")
                }
            }
var-request=URLRequest(url:urlFinal)
request.httpMethod=httpMethod.post.rawValue
request.allHTTPHeaderFields=dictHeader
request.timeoutInterval=10
request.httpBody=newPassword.data(使用:String.Encoding.utf8)
Alamofire.request(request).responseString{(response)in

如果response.response!.statusCode>=200&&response.response!.statusCode您不必将字典转换为JSON字符串,因为
Alamofire
可以进行编码,请参见此

我建议你把代码改成这样

do{
   let dictionary = try data.asDictionary()
   Alamofire.request(url, method: .post, parameters: dictionary, encoding: .JSON, headers: [:]).responseJSON { (res) in
        print(res.result)
        print("Request Data \(res.request) \n Dictionary \(jsonString)")
        do{
            let d = try JSONDecoder().decode([OTPMessage].self, from: res.data!)
            print(d[0].message)
        }catch{
            print(error)
        }
    }
} catch{
    print(error)
}