Moya: how to use task " requestCompositeParameters " with bodyEncoding which is URLEncoding

Created on 23 Dec 2017  Â·  10Comments  Â·  Source: Moya/Moya

I have a question when i use moya 10.0

in my task i want to post some parameters with form not json, so i use URLEncoding.default to encode bodyParameters.

but i found moya dont support this, and note me "URLEncoding is disallowed as bodyEncoding."
so how can i use URLEncoding to encode my bodyParameters like below.

 let paramams = ["stationId": stationId, "jsonData" : jsonData] as [String : Any]
            return .requestCompositeParameters(bodyParameters: paramams, bodyEncoding: URLEncoding.default, urlParameters: ["key" : key])
question

Most helpful comment

@sunshinejr @SD10 ,thanks a lot. I update Moya to 10.0.2 and it woks really well.

All 10 comments

Hey @fengerxixi, you could need to use .requestParameters instead of .requestCompositeParameters:

return .requestParameters(parameters: parameters, encoding: URLEncoding.default)

Because the only reason requestCompositeParameters exists is if you want to use url parameters and body parameters at once.

Thanks for your reply @sunshinejr . But my project is indeed to use url parameters and body parameters at once. At the same time my body parameters need to be encoded by URLEncoding, so .requestParameters cant resolve my problem。
Now i use .requestCompositeData to encode my bodyParameters. If .requestCompositeParameters can support bodyParameter encoded by URLEncoding that would be greate.

func formData(_ parameters: [String: Any]) -> Data {
        var components: [(String, String)] = []
        for key in parameters.keys.sorted(by: <) {
            let value = parameters[key]!
            components += URLEncoding.default.queryComponents(fromKey: key, value: value)
        }
        let bodyString = components.map { "\($0)=\($1)" }.joined(separator: "&")
        return bodyString.data(using: .utf8, allowLossyConversion: false)!
    }
let jsonData = post.toJSONString()!
  return .requestCompositeData(bodyData: formData(["jsonData": jsonData]), urlParameters: [ "key" : UserDefaults.LoginData.string(forKey: .key)!])

Interesting, I remember adding this restriction. @fengerxixi Why are you using URLEncoding for the body parameters?

@SD10 Because my service interface need body parameters to be a form data, so i need to use URLEncoding for the body parameters. The interface is a post method, and it needs body parameters and url parameters at once.

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 🙏 Also, feel free to open a new issue if you still experience this problem 👍.

Hmm, I guess we could remove the fatalError. What we really wanted was to avoid a situation where you would use URLEncoding.queryString as ParameterEncoding for body parameters (or URLEncoding.methodDependent where method would be get, head, delete). Right now we block users from encoding parameters using URLEncoding in the httpBody.

What we could do: instead of blocking URLEncoding as parameterEncoding for bodyParameters, we could block URLEncoding.methodDependent and URLEncoding.queryString and say that only URLEncoding we accept in bodyParameters is URLEncoding.httpBody.

Opinions, @fengerxixi @SD10?

@sunshinejr Thanks for not letting this slip by. I think that’s a great idea if it works for @fengerxixi

Sent with GitHawk

@fengerxixi Hey, just a heads-up that we enabled URLEncoding.httpBody as bodyEncoding argument. It is also released in the newest Moya 10.0.2, thanks to @SD10. If this doesn't resolve your issue, please let us know!

@sunshinejr @SD10 ,thanks a lot. I update Moya to 10.0.2 and it woks really well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mwawrusch picture mwawrusch  Â·  3Comments

kamwysoc picture kamwysoc  Â·  3Comments

PlutusCat picture PlutusCat  Â·  3Comments

fenixsolorzano picture fenixsolorzano  Â·  3Comments

syq7970 picture syq7970  Â·  3Comments