I'm new to moya. I've got a case as below. I need to use both path and JSON Encoding.
case updateCart(memberid: String, Cartidx: String, Num: Int)
As memberid goes to the path.
case .updateCart(let memberid):
return "Cart/\(memberid)"
and Cartidx & Num goes as JSON
case let .updateCart(_, Cartidx: Cartidx, Num: Num):
return .requestParameters(parameters: ["Cartidx": Cartidx, "Num" : Num], encoding: JSONEncoding.default)
However, it doesn't seem to work. I tested it by removing 'memberid' and embed a fake one, such as cxxxx-xxxx-xxxxxxxx, temporarily. As it turned to be like
case updateCart(Cartidx: String, Num: Int)
case .updateCart:
return "Cart/cxxxx-xxxx-xxxxxxxx"
case let .updateCart(Cartidx: Cartidx, Num: Num):
return .requestParameters(parameters: ["Cartidx": Cartidx, "Num" : Num], encoding: JSONEncoding.default)
It worked in that way, but I'm still not able to solve the problem. I supposed the two things somehow conflict. My question is that how can I make Path and JSON both work in this case?
Hey @sunnyleeyun. Have you maybe tried using requestCompositeParameters? It should allow you using parameters both in URL (with URLEncoding) and in body (JSONEncoding in that case).
@sunshinejr As a matter of fact, I did. Maybe I misunderstood. Before answering this, I tried:
case let .updateCart(_, Cartidx: Cartidx, Num: Num):
return .requestCompositeParameters(bodyParameters: ["Cartidx": Cartidx, "Num" : Num], bodyEncoding: JSONEncoding.default, urlParameters: [:])
Clear error. (status code: 400)
However, after reviewing your answer, I tried again.
case let .updateCart(memberid: memberid, Cartidx: Cartidx, Num: Num):
return .requestCompositeParameters(bodyParameters: ["Cartidx": Cartidx, "Num" : Num], bodyEncoding: JSONEncoding.default, urlParameters: ["memberid": memberid])
Still an error. (status code: 405 -> wrong method or wrong parameter. In this case, probably wrong parameter). Actually, I encountered this error before when missing "path".
I supposed it referred to two completely different result. I'm not so sure how exactly requestCompositeParameters worked, but it seems kind of strange putting ["memberid": memberid] in the urlParameters. How can I adjust above request to the "path" of like solution? Thanks a lot!
Okay, it seems like you want URL like https://url.to/path/MEMBER_ID instead of https://url.to/path?memberId=MEMBER_ID so it's really a custom situation.
Then you should probably use .requestParameters(parameters: ["Cartidx": Cartidx, "Num" : Num], encoding: JSONEncoding.default) and have memberId in the path as you had before. Probably, though, you would need to make an encoding of your path by yourself.
One thing that could help you with debugging your requests/responses is our NetworkLoggerPlugin. In your MoyaProvider instance add plugins parameter:
let provider = MoyaProvider<Endpoint>(plugins: [NetworkLoggerPlugin(verbose: true)])
This way you will get logs on the console every time there is a request/response. You could then copy/paste your request and tell us exactly what are you sending in URL and what is the expected behaviour.
@sunshinejr Hello, I think. It's like my question #1647
Yes Path and JSON Body work together
@SeRG1k17 I don't think it's the same. The path I meant was not "query string".
@kaleemozitech How exactly?
@sunshinejr Yes, I'd like url as https://url.to/path/MEMBER_ID , but what do you mean "Probably, though, you would need to make an encoding of your path by yourself." I supposed I'm doing things half right. Is there any example I could take as an example?
@sunnyleeyun You can鈥檛 use dynamic formatting in path variable.
@SeRG1k17 It's possible to use return "Cart/\(memberid)" at the path target, so I supposed this means we can use dynamic formatting in path variable, but perhaps not combining with dynamic task target.
@sunnyleeyun Sure you can use dynamic formatting, but it incorrect encoding for task
@sunnyleeyun Can you configure the path in the baseUrl instead?
@sunnyleeyun this is how exactly
var path: String {
switch self {
case let .cities(str:str):
return "/home/cities/\(str)"
default:
return "/home"
}
}
i am not taking about query string URL encoding
i am talking about appending var in URL path
@sunnyleeyun did you get success
here is one more answer
https://github.com/Moya/Moya/issues/909
here is one more Answer if you want to use Both JSON and URL Encoding
https://github.com/Moya/Moya/issues/1059
@kaleemozitech Thanks for your reply. Can you take a look at my question above? Cause it seems that I did exactly as you said at the very beginning. However, it wasn't working. Perhaps there's something I missed or went wrong which I didn't recognized.
@sunnyleeyun i am also facing one issue(https://github.com/Moya/Moya/issues/1653) for URL Encoded and JSON Encoded parameters and i tried to use requestCompositeParameters but failed then uploadCompositeMultipart worked perfectly
let avatarObj = MultipartFormData(provider: .data(data), name: "avatar", fileName: "avatar.jpg", mimeType: "image/jpg")
let nameObj = MultipartFormData(provider: .data(name.data(using: .utf8)!), name: "name")
let genderObj = MultipartFormData(provider: .data("\(gender)".data(using: .utf8)!), name: "gender")
let date_of_birthObj = MultipartFormData(provider: .data(date_of_birth.data(using: .utf8)!), name: "date_of_birth")
let cityObj = MultipartFormData(provider: .data("\(city)".data(using: .utf8)!), name: "city")
let addressObj = MultipartFormData(provider: .data(address.data(using: .utf8)!), name: "address")
// let blog_notification_status = MultipartFormData(provider: .data(name.data(using: .utf8)!), name: "name")
let feeObj = MultipartFormData(provider: .data("\(fee)".data(using: .utf8)!), name: "fee")
let servicesObj = MultipartFormData(provider: .data(services.data(using: .utf8)!), name: "services")
let bioObj = MultipartFormData(provider: .data(bio.data(using: .utf8)!), name: "bio")
let educationObj = MultipartFormData(provider: .data(education.data(using: .utf8)!), name: "education")
let specialitiesObj = MultipartFormData(provider: .data("\(specialities)".data(using: .utf8)!), name: "specialities[]")
let twitterObj = MultipartFormData(provider: .data(twitter.data(using: .utf8)!), name: "twitter")
let websiteObj = MultipartFormData(provider: .data(website.data(using: .utf8)!), name: "website")
let facebookObj = MultipartFormData(provider: .data(facebook.data(using: .utf8)!), name: "facebook")
let google_plusObj = MultipartFormData(provider: .data(google_plus.data(using: .utf8)!), name: "google_plus")
let linked_inObj = MultipartFormData(provider: .data(linked_in.data(using: .utf8)!), name: "linked_in")
let multipartData = [avatarObj, nameObj, genderObj, date_of_birthObj, cityObj, addressObj, feeObj, servicesObj, bioObj, educationObj,specialitiesObj, twitterObj, websiteObj, facebookObj, google_plusObj, linked_inObj]
// return .uploadMultipart(multipartData)
return .uploadCompositeMultipart(multipartData, urlParameters: ["_method" : "put"])`
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 馃憤.
Most helpful comment
@sunshinejr As a matter of fact, I did. Maybe I misunderstood. Before answering this, I tried:
Clear error. (status code: 400)
However, after reviewing your answer, I tried again.
Still an error. (status code: 405 -> wrong method or wrong parameter. In this case, probably wrong parameter). Actually, I encountered this error before when missing "path".
I supposed it referred to two completely different result. I'm not so sure how exactly requestCompositeParameters worked, but it seems kind of strange putting ["memberid": memberid] in the urlParameters. How can I adjust above request to the "path" of like solution? Thanks a lot!