Using asp.net web api v2, I have a working POST method that I am able to POST a custom type from a different application, and by using JSONConvert I am able to deserialize it and use it in my POST method body.
However, the parameter to my POST must be of type "object" or else the parameter is not found (null).
Why does this happen? I would ideally have the custom type as the parameter type, so that my API Documentation can populate with the proper request info, since it auto-generates the API docs based on the parameter type used (don't see a way to override that -- would be great if that was possible).
See my code below -- if "incomingInformation" is of type "RemoteFileInfo" rather than type "object", a null exception is thrown when I try to .toString() it.
[Route("api/xx/uploadfiletoalfresco/")]
[HttpPost()]
public ResultStruct UploadFileToAlfresco(object incomingInformation)
{
JObject deserializedJObject = (JObject)JsonConvert.DeserializeObject(incomingInformation.ToString());
SA.Services.RemoteFileInfo convertedRemoteFileInfo = deserializedJObject.ToObject<SA.Services.RemoteFileInfo>();
...
Here is my sample code on the sending application (vb.net) - the content type is set as application/json and is serialized before sending
Dim req As WebRequest = WebRequest.Create(_restEndpointURL & "/uploadfiletoalfresco/")
req.ContentType = "application/json"
req.Method = "POST"
Using sw As New StreamWriter(req.GetRequestStream())
Dim ser As New JavaScriptSerializer
Dim serJSON = ser.Serialize(JsonConvert.SerializeObject(remoteFileInfo))
sw.Write(serJSON)
sw.Flush()
sw.Close()
End Using
Below is my remoteFileInfo type, it is declared this way on both the receiving app and sending app. It is converted to JSON string before sending by the method JsonConvert.SerializeObject
Partial Public Class RemoteFileInfo
Public CategoryID As Integer
Public FileName As String
Public Length As Long
Public Note As String
Public SFSubmissionID As String
Public SourceInstance As String
Public Subject As String
Public UserID As Integer
Public Visibility As Boolean
Public riskID As Integer
Public fileByteArray As Byte()
End Class
Receiving app definition:
public class RemoteFileInfo
{
public int CategoryID;
public string FileName;
public long Length;
public string Note;
public string SFSubmissionID;
public string SourceInstance;
public string Subject;
public int UserID;
public bool Visibility;
public int riskID;
public Byte[] fileByteArray;
}
Sample JSON from the sending application:
"{"CategoryID":2,"FileName":"Scrum postponed until this afternoon .msg","Length":62976,"Note":"asdf","SFSubmissionID":"006E000000OuYxP","SourceInstance":"Addin","Subject":"Scrum postponed until this afternoon ","UserID":0,"Visibility":true,"riskID":0,"fileByteArray":"VERY LONG STRING"}"
Full JSON from fiddler:
POST http://yyyy/api/xxx/uploadfiletoalfresco/ HTTP/1.1
Content-Type: application/json
Host: yyyyy
Content-Length: 84273
Expect: 100-continue
Connection: Keep-Alive
"{\"CategoryID\":2,\"FileName\":\"Scrum postponed until this afternoon .msg\",\"Length\":62976,\"Note\":\"asdf\",\"SFSubmissionID\":\"006E000000OuYxP\",\"SourceInstance\":\"Addin\",\"Subject\":\"Scrum postponed until this afternoon \",\"UserID\":0,\"Visibility\":true,\"riskID\":0,\"fileByteArray\":\"VERY LONG STRING - user edited this is not how it looks in fiddler!\"}"