I need to call my WCF REST Service with multiple parameters with the POST method, but I can't create DataContract containing my parameters because I need simple types : my webservice will be consumed by an objective C application.
I found this syntax on the MSDN site :
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "savejson?id={id}&fichier={fichier}")]
bool SaveJSONData(string id, string fichier);
To explain quickly the context, I have to call this method to save a JSON file with the Id passed on a database.
My fisrt question is : is it really possible to pass several parameters to a POST method as shown before ?
Secondly : how can I do to consume my service (in C# for the moment, just to test it) with several parameters ?
I've already tested with DataContract, and I was doing like that :
string url = "http://localhost:62240/iECVService.svc/savejson";
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json; charset=utf-8";
RequestData reqData = new RequestData { IdFichier = "15", Fichier = System.IO.File.ReadAllText(@"C:\Dev\iECV\iECVMvcApplication\Content\fichier.json") };
MemoryStream requestMs = new MemoryStream();
DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(RequestData));
requestSerializer.WriteObject(requestMs, reqData);
byte[] responseData = webClient.UploadData(url, "POST", requestMs.ToArray());
MemoryStream responseMs = new MemoryStream(responseData);
DataContractJsonSerializer responseSerializer = new DataContractJsonSerializer(typeof(ResponseData));
ResponseData resData = responseSerializer.ReadObject(responseMs) as ResponseData;
RequestData and ResponseData were declared this way :
[DataContract(Namespace = "")]
public class RequestData
{
[DataMember]
public string IdFichier { get; set; }
[DataMember]
public string Fichier { get; set; }
}
[DataContract]
public class ResponseData
{
[DataMember]
public bool Succes { get; set; }
}
But as I said, I can't do it like this anymore...
I hope I'm enough clear, if not, don't hesitate to ask me details !
Thanks a lot for your help.