I am using RestSharp (version 104.4 via NuGet) to make calls to a Rest Web Service. I have designed a set of objects (POCO) which matches resources exposed in the API. However, my objects property names does not match those expected by the Rest Service when posting data, so I would like to "transform" them when I make a request to the Rest service to make them match match. I read that adding SerializeAs
attribute (with a Name specified) on my POCO's property will make them serialize correctly, but it won't.
My POCO
Imports RestSharp.Serializers
<Serializable(), SerializeAs(Name:="ApiMember")>
Public Class ApiMember
<SerializeAs(Name:="id")>
Public Property Id As Integer?
<SerializeAs(Name:="email")>
Public Property EmailAddress As String
<SerializeAs(Name:="firstname")>
Public Property Firstname As String
<SerializeAs(Name:="lastname")>
Public Property Lastname As String
End Class
My Call to the API
Dim request As RestRequest = New RestRequest(Method.POST)
Dim member As ApiMember = new ApiMember()
member.EmailAddress = "me@example.com"
request.Resource = "members"
request.RequestFormat = DataFormat.Json
request.AddBody(member)
Dim client As RestClient = New RestClient()
client.BaseUrl = "http://url.com"
client.Authenticator = New HttpBasicAuthenticator("username", "password")
client.Execute(Of ApiGenericResponse)(request)
What ends up being posted
{"Id":null,"EmailAddress":"me@example.com","Firstname":null,"Lastname":null}
Notice the name of the properties does not match thoses I specified in SerializeAs
(uppercases, name of EmailAddress)
Am I missing something ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…