Alamofire file upload getting error “JSON text did not start with array or object and option to allow fragments not set”

吃可爱长大的小学妹 提交于 2019-12-11 07:39:03

问题


Below is my code referring this question answer

func createRequest(ResumeID: String, CandidateID: String, MediaName: String, FileExtension : String, MediaType : String) throws -> URLRequest {

    let parameters = NSDictionary(objects: [ResumeID, CandidateID, MediaName, FileExtension,MediaType], forKeys: ["ResumeID" as NSCopying, "CandidateID" as NSCopying, "MediaName" as NSCopying, "FileExtension" as NSCopying, "MediaType" as NSCopying])

    let boundary = generateBoundaryString()

    let url = URL(string: "http://192.168.1.29/ColorsKit_New_Svr/WTCSvr.svc/WTCService?Id=6&SPName=Usp_RTN_IU_CandidateSubmissionResume")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    let path1 = Bundle.main.path(forResource: "dummy-pdf_2", ofType: "pdf")!
    request.httpBody = try createBody(with: parameters as? [String : String], filePathKey: "MediaContent", paths: [path1], boundary: boundary)

    return request
}

private func createBody(with parameters: [String: String]?, filePathKey: String, paths: [String], boundary: String) throws -> Data {
    var body = Data()

    if parameters != nil {
        for (key, value) in parameters! {
            body.append("--\(boundary)\r\n")
            body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.append("\(value)\r\n")
        }
    }

    for path in paths {
        let url = URL(fileURLWithPath: path)
        let filename = url.lastPathComponent
        let data = try Data(contentsOf: url)
        let mimetype = mimeType(for: path)

        body.append("--\(boundary)\r\n")
        body.append("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename)\"\r\n")
        body.append("Content-Type: \(mimetype)\r\n\r\n")
        body.append(data)
        body.append("\r\n")
    }

    body.append("--\(boundary)--\r\n")
    return body
}

func sendMultipartRequest() {
    let request: URLRequest

    do {
        request = try createRequest(ResumeID: "1", CandidateID: "1241124", MediaName: "dummy-pdf", FileExtension: "pdf", MediaType: "pdf")
    } catch {
        print(error)
        return
    }

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard error == nil else {
            // handle error here
            print(error!)
            return
        }

        // if response was JSON, then parse it

        do {                
            let responseDictionary = try JSONSerialization.jsonObject(with: data!)
            print("success == \(responseDictionary)")

            // note, if you want to update the UI, make sure to dispatch that to the main queue, e.g.:
            //
            // DispatchQueue.main.async {
            //     // update your UI and model objects here
            // }
        } catch {
            print(error)

            let responseString = String(data: data!, encoding: .utf8)
            print("responseString = \(String(describing: responseString))")
        }
    }
    task.resume()
}

The Response I'm getting is:

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.} responseString = Optional("No Records Found")

This is strange because Postman is giving correct response. Means there is something missing in the code only :(


回答1:


Use Alamofire

let upload_url = "your url"
let fieldName = "UploadedFile"
let mimeType = "plain/text"


Alamofire.upload(multipartFormData: { multipartFormData in

//you can add multiple file
        multipartFormData.append(fileData as Data, withName: fieldName, fileName: fileName, mimeType: mimeType)

    }, to: upload_url, method: .post, headers: ["Authorization": "auth_token"],

       encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.response { [weak self] response in
                guard let _ = self else {
                    return
                }
                debugPrint(response)
            }
        case .failure(let encodingError):
            debugPrint("uploaderService error:\(encodingError)")
        }

    })



回答2:


Use JSONSerialization as below

if let responseDictionary = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? String {

...
}


来源:https://stackoverflow.com/questions/48220802/alamofire-file-upload-getting-error-json-text-did-not-start-with-array-or-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!