Moya: Unable to post correct request parameter with multipart Body

Created on 21 Sep 2016  路  2Comments  路  Source: Moya/Moya

@leoneparise and @sunshinejr

the browser shows following request payload after the file upload

------WebKitFormBoundarycYWpVxvmArIdBkb5
Content-Disposition: form-data; name="__operation"

upload
------WebKitFormBoundarycYWpVxvmArIdBkb5
Content-Disposition: form-data; name="upFile"; filename="IMG_6543.JPG"
Content-Type: image/jpeg


------WebKitFormBoundarycYWpVxvmArIdBkb5--

and sent to following Request URL

https://localhost:3000/api/handler/fileviewer/slave?__form_token=g36dn786ab&relkind=datatemplate.message&filelinkkind=&target=-1&tmpId=-1

to accommodate above request I have made following request

enum TestApi {
    ...
    case PostFile(imageFile: AnyObject, token: String)
    ...
}

extension TestApi: TargetType {

    var baseURL: NSURL {
        switch self {
        case .PostFile:
            return NSURL(string: "http://192.168.9.121:3000")!
        }
    }

    var path: String {
        switch self {
        case .PostFile(_, let token):
            return "/handler/fileviewer/slave?__form_token=" + token + "&relkind=datatemplate.message&filelinkkind=&target=-1&tmpId=-1"
        }
    }

    var method: Moya.Method {
        switch self {
        case .PostFile:
            return .POST
        }
    }

    var parameters: [String: AnyObject]? {
        switch self {
        case .PostFile(_, _):
            return ["__operation":"upload"]
        }
    }

    var sampleData: NSData {
        return "".dataUsingEncoding(NSUTF8StringEncoding)!
    }

    var multipartBody: [MultipartFormData]? {
        switch self {
        case .PostFile(let imageFile, _):
            guard let data = UIImageJPEGRepresentation(imageFile as! UIImage, 1.0) else { return nil }
            return [MultipartFormData(provider: .Data(data), name: "upFile", mimeType:"image/jpeg", fileName: "photo.jpg")]

        default:
            return []
        }
    }
}
question

All 2 comments

ok the issue was with my the endpoint construction, I had to stringByRemovingPercentEncoding my url that was encoding my ? character in url

Glad you got it figured out! Thanks for posting your answer, I'm sure it'll be of help to someone.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedrovereza picture pedrovereza  路  3Comments

ghost picture ghost  路  3Comments

gunterhager picture gunterhager  路  3Comments

mwawrusch picture mwawrusch  路  3Comments

hjzCy picture hjzCy  路  3Comments