Hi,
I'm trying to send a request with a boolean value, so I configured my Target like this:
public enum MyAPI {
...
case Foo(NSNumber, String, String, String, Bool)
...
}
My parameters:
public var parameters: [String: AnyObject]? {
switch self {
...
case .Foo(_, let data1, let data2, let data3, let boolData):
return [
"data1": data1,
"data2": data2,
"data3": data3,
"somethingBool": Int(boolData)
]
default:
return nil
}
}
Problem:
My request is always sending false as the value.
What about
public var parameters: [String: AnyObject]? {
switch self {
...
case .Foo(_, let data1, let data2, let data3, bool data4):
return [
"data1": data1,
"data2": data2,
"data3": data3,
"somethingBool": data4 ? 1 : 0
]
default:
return nil
}
}
You should be able to just have "somethingBool": data4 as an element in the returned dictionary, right?
@ashfurrow: Sorry, I forgot to mention it. That was my first attempt, and it also sends false there.
I've tried to make the request via postman and it succeeded with the proper value. Maybe the specification of the request helps to detect if it's a server error or Moya's configuration one.
Thanks for the follow-up. How are you detecting the value, on the server side or client side? Could you try using the network activity plugin to log the request before it's sent?
@ashfurrow I'm detecting it on the client side. The parameter is being sent as 0 or 1 but I guess I'm missing to track the real request body being sent to the server. I'll try to use that plugin and see what happens
@jasl I've tried to do what you've said and got the same result.
I've finally managed to save the value by sending the value as a string: "true" or "false", but I'm not really sure what's causing the boolean to fail.
public var parameters: [String: AnyObject]? {
switch self {
...
case .Foo(_, let data1, let data2, let data3, bool data4):
return [
"data1": data1,
"data2": data2,
"data3": data3,
"somethingBool": "\(data4)"
]
default:
return nil
}
}
@lucasp90
Sorry my previous answer is wrong.
Both URI query and form are only support string, other types should be encoded to string (Moya can help you do some, but not all, details here) .
There are three common practices for passing boolean value to server side:
"1" for true, "0" for false"t" for true, "f" for false"true" for true, "false" for falseChoose what is depending your server side, if you passing string "true" or "false" to server can get right response, just keeping the way.
@lucasp90 That's certainly puzzling! Especially since Moya gets Alamofire to do the parameter encoding, and I would have thought they would support Bool.
@jasl Good thinking linking to that. It seems to fallback to String interpretation of plain objects, so Bool should work, right?
@ashfurrow
I think better not do this, because there haven't a standard practice for passing boolean value yet, different web framework may have different convention.
I dunno, I think that if Alamofire is parsing Bool as "true" or "false" it must be a good approach. These Wordpress docs explicitly say that either is acceptable for them.
@ashfurrow
Ruby on Rails (one of the most famous web framework) both using 1/ 0 and t / f
@lucasp90 has this been clarified to your satisfaction?
Oh, sorry! I thought I'd answered after my issue was detected!
I think my question has been properly answered :)
Would it be a good idea to add examples about passing different parameter types to an endpoint?
No problem! I think adding examples would be a great idea, where do you think they should go? https://github.com/Moya/Moya/blob/master/docs/Targets.md maybe?
I think that's a good place for them.
Cool! Could I ask you to send a pull request? Don't worry if you can't, we'll get it :+1:
Yeah, of course!! 馃憤
Closing this as it looks like it's been resolved 馃槃 thanks for contributing the README again, @lucasp90!
Most helpful comment
Yeah, of course!! 馃憤