none
WCF REST, Post multipart body RRS feed

  • Question

  • Is it possible to create a WCF Rest client, that serialize some objects into a JSON formatted string and then post this string as/in multipart body to the service..?? Well, I'm pretty sure it's possible, I just cant figure out how to do it right.

    There are mainly two things I need help with - I know how to create a WCF client/service, but not how to create it as a REST service. The other thing is how to create the multipart body. Do I first serialize the objects into a json stream and then into a multipart body, or is it all a all-in-one operation ??

    Thank you in advance.

    /Peter

    Wednesday, January 22, 2014 5:48 PM

Answers

  • Hi.

    There is no built-in multipart body parser prior .net 4.5. Look here for more info (search for multipart part). I had similiar problem for .net 4.0 and found few solutions on github (search github by key words). On server side you accept single stream parameter and then use appropriate library API. On client side look for FormData object. You can find more client examples here and here.

    Wednesday, January 22, 2014 6:53 PM
  • Hi.

    Maybe this can be useful. Also look here and here.

    Thursday, January 23, 2014 9:46 AM
  • On a server side I had following:

     [WebInvoke(Method = "POST", UriTemplate = "/p", ResponseFormat = WebMessageFormat.Json)]
    public string Test(Stream stream)
     {
          MultipartFormDataParser s = new MultipartFormDataParser();
                s.ParseStream(stream);
                return "ok";
       }

    On client I had a following code:

    public static void Main()
    {
     
       var dict = new Dictionary<string, string> {{"test1", 
    "value1"}, {"test2", "value2"}, {"test3", "value3"}};
    
       MultipartFormDataPost("http://localhost:6001/Customers/p", dict);
    
     }

    Service code use this library; client code is based in this snippet.

    Monday, January 27, 2014 11:04 AM

All replies

  • Hi.

    There is no built-in multipart body parser prior .net 4.5. Look here for more info (search for multipart part). I had similiar problem for .net 4.0 and found few solutions on github (search github by key words). On server side you accept single stream parameter and then use appropriate library API. On client side look for FormData object. You can find more client examples here and here.

    Wednesday, January 22, 2014 6:53 PM
  • Hi Sharov - thanks for your input.

    I have no problem doing this using JQuery from a webpage. The problem is, that i need to do this in code :-(

    I have a simple test without any WCF, but I don't know how to form the json formatted string into a multiform object - or if it's possible at all to create the request this way..:

    Request request = new Request()
    {
        Id = 100,
        Subscriber = new Subscribe()
        {
            Name = "My Name",
        },
    };
    
    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Request));
    serializer.WriteObject(stream, request);
    stream.Position = 0;
    StreamReader streamReader = new StreamReader(stream);
    
    HttpWebRequest requestObj = WebRequest.Create(url) as HttpWebRequest;
    requestObj.Method = "POST";
    requestObj.ServicePoint.Expect100Continue = false;
    requestObj.Timeout = 20000;
    requestObj.ContentType = "multipart/form-data";
    
    using (StreamWriter requestWriter = new StreamWriter(requestObj.GetRequestStream()))
    {
        requestWriter.Write(streamReader.ReadToEnd());
    }
    

    Thursday, January 23, 2014 7:04 AM
  • Hi.

    Maybe this can be useful. Also look here and here.

    Thursday, January 23, 2014 9:46 AM
  • Hi,

    It does help indeed, but I still need to know if its possible to make this call using WCF !?!

    Friday, January 24, 2014 3:04 PM
  • On a server side I had following:

     [WebInvoke(Method = "POST", UriTemplate = "/p", ResponseFormat = WebMessageFormat.Json)]
    public string Test(Stream stream)
     {
          MultipartFormDataParser s = new MultipartFormDataParser();
                s.ParseStream(stream);
                return "ok";
       }

    On client I had a following code:

    public static void Main()
    {
     
       var dict = new Dictionary<string, string> {{"test1", 
    "value1"}, {"test2", "value2"}, {"test3", "value3"}};
    
       MultipartFormDataPost("http://localhost:6001/Customers/p", dict);
    
     }

    Service code use this library; client code is based in this snippet.

    Monday, January 27, 2014 11:04 AM