Sorry I must not be conveying my point very well. I'm very aware of json serialization.
I'm stuck trying to use two different generic types in the same function. One is for the request body. The other is for the response.
If I remove the request body argument... everything is fine. I get a response and it uses codable to build my model object. Perfect.
If I add another generic type to the function (same request body argument, two total), things start to get confused. I was thinking it might be nice to just build a model and pass that into the function as opposed to a dictionary for the body.
Now I'm confused again. What response? I mean, what is the value you expect, and what is its type?
Unless I'm completely missing something, you're entirely in control of what gets passed as the first parameter of the completion handler, which you call post. In the non-error case, the completion handler gets invoked here:
You are, in fact, decoding the data from the response body into a value of type T (TestBody in this case), and that's what you're gonna print. You are passing the URLResponse as the second parameter, but that's not what you print.
Hmm, maybe I'm starting to see…
Note that the data you're decoding (in the above line of code) is the response body, not the request body. If you're expecting a different type of value as the response (which does seem like the most usual case for a REST API), then either the server is sending you the wrong thing (it's sending a T), or you're decoding it as the wrong type.
If the response has a different body value type from the request, but you can't predict what the type is going to be, then you can't solve this directly using generics. If that's the problem, we can discuss alternatives (I've run into this too), but let's clear up the first source of confusion first.