0

I am working on a project that requires the use of curl commands to load a file to a specific site. I want this to be implemented using C#. From looking at similar questions on SO I have not found anything that seems to work for me, unless I am doing this wrong.

This is the curl command I successfully use with Cygwin:

curl -i -u user:password -F 'file1=@\Users\User\Desktop\test.jpg' -F 'json={"content": {"type": "text/html", "text": "<body><p>look at me, look at me</p></body>"}, "type": "document", "subject": "Document with Attachment"};type=application/json' http://someurl

Right now I am trying to make this work with a WebRequest, as I've read that this was possible on other posts. This is what I have thus far.

 WebRequest request = WebRequest.Create("http://someurl");
            request.PreAuthenticate = true;
            request.ContentType = "application/json";
            request.Method = "POST";
            string authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes("user:password"));
            request.Headers["Authorization"] = "Basic " + authInfo;

            //what do I set my buffer to?

            Stream reqstr = request.GetRequestStream();
            reqstr.Write(buffer, 0, buffer.Length);
            reqstr.Close();

            WebResponse response = request.GetResponse();

What do I set my buffer to here to write the curl command which in turn should send the file test.jpg? Also, I tried using libcurl without success either. Any pointers in the right direction are greatly appreciated!

swabs
  • 515
  • 1
  • 5
  • 19
  • You must write a content of type multipart/form-data (the -F parameter of curl send 2 field, a json and a file). Here an example: http://stackoverflow.com/a/3890955/209727 – Davide Icardi Mar 24 '13 at 22:07
  • dump all that auth stuff. request.Credentials = new NetworkCredential(user,pass); – Sam Axe Mar 24 '13 at 22:13

1 Answers1

0

You can either first POST json document description, and then PUT file contents with second request, or upload both with single POST, using multipart/form-data.

This post has great example how to do this: How do I add/create/insert files to Google Drive through the API?

Community
  • 1
  • 1
Alexander
  • 4,153
  • 1
  • 24
  • 37