0

I have the following python code how to post a picture through webservice:

product_image = requests.post(
'https://client.planorama.com/tapi/v1/product_image/', 
data={ 'product_id': 1784682 }, 
files={ "file": open(my_image.jpg, 'rb') } 
)

Can anyone help me to do the same thing in C#,

Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73

2 Answers2

0

We are rather spoiled with the requests library in python.

Are you running .NET 4 or 4.5? If so, take a look at Joshcodes answer to Sending Files using HTTP POST in c# - it uses Microsoft.Net.Http which is by far the best HTTP library in the .NET world these days.

UPDATE: I haven't checked this for accuracy yet, but it could go something like this:

static HttpResponseMessage UploadFileWithParam(string requestUri, string fileName, string key1, string val1)
{
    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            content.Add(new StringContent(val1), key1);
            var fileContent = new StreamContent(File.OpenRead(fileName));
            fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName
            };
            content.Add(fileContent);
            return client.PostAsync(requestUri, content).Result;
        }
    }
}

// UploadFileWithParam("http://example.com", @"c:\...", "param1", "value1").Dump();
Community
  • 1
  • 1
Aaron
  • 2,341
  • 21
  • 27
  • thank you for the response. The proble that I have is: I have to pass two parameters which are the productId and the file(picture). in python it's easy but in .net I know how to read the image using filestream but I don't how to pass it as a parameter value – user2143201 Mar 19 '14 at 07:18
  • I've added a snippet above that might help you. I'd try it out with LINQPad (https://www.linqpad.net) and compare the requests between the python and the .NET version via fiddler (http://www.telerik.com/fiddler). – Aaron Mar 19 '14 at 08:44
  • Thank you for the Update. I'll try it. hopefully it will solve my issue – user2143201 Mar 19 '14 at 09:29
0

Multipart Form Post in C# includes a simple C# class using HttpWebRequest with a provided (working) example.

0x8BADF00D
  • 962
  • 1
  • 8
  • 18
  • thank you for the link. but it doesn't show how to post a picture as a parameter value. the parameter name is file, how to pass the picture as file value? – user2143201 Mar 19 '14 at 07:29
  • It does: `postParameters.Add("file", new FormUpload.FileParameter(data, "my_image.jpg", "image/jpg"));` (data being the filestream containing the picture) is what you want – 0x8BADF00D Mar 19 '14 at 07:33
  • Thank you, that's exactly what I want but will I use the same method to pass the productId? because the file and productId have to be all posted. I also need to use credentials to get access. how do I go about? – user2143201 Mar 19 '14 at 07:50
  • As far as I can see from your question, the product_id not a file and should therefore be provided as normal post parameter. Take a look at the class, the author made some comments about authentification. – 0x8BADF00D Mar 19 '14 at 07:59
  • Thank you. I'll try it. I hope it's going to work because I've struggling for days – user2143201 Mar 19 '14 at 08:08