如何在Swift中解析Alamofire multipartFormData响应

时间:2018-01-10 10:13:50

标签: swift alamofire multipartform-data

尝试处理来自Alamofire的响应,但响应是multipartFormData,我无法解析它。我对make请求没有任何问题。当我检查服务日志时,我可以在multipartFormData中正确发送我的请求,并在multiparFormData中也接收true响应。 Alamofire给我回复response.result, response.data, response.value,但没有一个能帮助我。有一些像Json部分和wav文件作为回应,我需要单独采取它们。 (顺便解析它就像json不起作用,响应不是json。)这是我的代码;

Alamofire.upload(multipartFormData:{ multipartFormData in


    multipartFormData.append( (jsonString?.data(using: String.Encoding.utf8)!)!, withName: "Json")


    if let resourceUrl = Bundle.main.url(forResource: "Path", withExtension: "wav") {
    if FileManager.default.fileExists(atPath: resourceUrl.path) {
                    print("file found")
                    multipartFormData.append((resourceUrl),withName:"AudioForSr")

                }
            }

        },
       usingThreshold:UInt64.init(),
       to:"http://192.168.10.***:30***/***/***/**/Run",
       method:.post,
       headers:["Authorization": strToken],
       encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):

       upload.responseString { response in
            debugPrint(response.result)
            debugPrint(response.data!)
            debugPrint(response.response)
            debugPrint(response.timeline)
            debugPrint(response.value)
       }

    case .failure(let encodingError):
        print(encodingError)
    }
})

debugPrint(Response.result)输出

SUCCESS: --38596e3e-346b-4d0d-8344-408ed12d99bf

Content-Type: application/json; charset=utf-8

Content-Disposition: form-data; name=Json



{"Type":"message","Id":null,"Timestamp":"2018-01-10T10:04:49.6035939Z","ServiceUrl":"localhost","LocalTimestamp":null,"ChannelId":"Skype","From":{"Id":"123","Name":"Jane ***"},"Conversation":{"IsGroup":null,"Id":"7bb20efb-43a2-44aa-b041-89dd4187284e","Name":null},"Recipient":{"Id":"555","Name":"John Doe"},"Locale":null,"TextFormat":null,"Text":"Peki hangi demomuz ile devam etmek istersiniz?","Speak":null,"Summary":null,"AttachmentLayout":null,"Attachments":[],"Entities":null,"ChannelData":{"ProjectName":"","AudioOutput":{"Generate":"true","PutInStructuredPart":"false","ExpectedFormat":"audio/wav"},"GenerateChatOutput":"true"},"ReplyToId":null,"Code":null,"IsSuccessful":true,"ErrorCode":"","ErrorMessage":""}

--38596e3e-346b-4d0d-8344-408ed12d99bf

Content-Type: audio/wav

Content-Disposition: attachment; filename=AudioFromTts.wav; name=AudioFromTts

Content-Encoding: binary



RIFFp»\277´ÿ¦ÿªÿ®ÿ¦ÿ¤ÿ¯ÿ§ÿ³ÿ¹ÿ¸ÿÍÿÛÿÚÿÚÿÒÿáÿñ  going on....

(和wav数据一样,我没有粘贴所有内容)。

0 个答案:

没有答案