Take the 2-minute tour ×

I read documentation of Salesforce Chatter REST API and started to implement code in c#. See following code:

System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
        req.Method = "POST";
        req.Headers.Add("Authorization: OAuth " + accessToken);
        string par = 
               "fileName=" + fileName +
           + @"D:\\MyFiles\\NewTextDocument.txt" +                  
                     "&desc=" + desc+
                     "&text=" + text;

        byte[] byteArray = Encoding.UTF8.GetBytes(par);
        req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);

        System.Net.WebResponse resp = req.GetResponse();

I am gettig error on response The remote server returned an error: (400) Bad Request.

If i see response of error, i got following message:

Please specify a file to upload. Type in the path to the file, or use the \"Browse\" button to locate it in your local filesystem.

I have already defined the file path and name. I tried with and without @ sign before path string but getting same error. Let me know if anything is missing.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can easily use Fiddler to see what's going on.

You are posting a simple form where fileName and feedItemFileUpload are just like desc and text, in other words, plain simple text!

What you need to do is send the file as a stream.

I can see that you're using Hanselman's code, but that's only for text parameters

for more information on using it for files, see this answer

Upload files with HTTPWebrequest (multipart/form-data)

share|improve this answer
I passed file contents but still getting same error...salesforce.com/us/developer/docs/chatterapipre/… –  Web World Sep 8 '11 at 7:22
from page 37 of that document it specifies that you need to use JSON or XML, plus they specify that you need to use multipart/form-data and in your example you are using application/x-www-form-urlencoded... you're not using either! As I can see, you're not very familiar with consuming Services, I would recommend that you ask help from Sales Force as you are probably a paid costumer / developer. –  balexandre Sep 8 '11 at 7:30
I have to convert following Java code to c# github.com/developerforce/Chatter-REST-API/wiki/… –  Web World Sep 8 '11 at 8:58
Then create a new question an ask that question directly, be nice how to ask or it will be closed for sure if you simply say "can you guys help me converting this into c#"... –  balexandre Sep 8 '11 at 9:54
add comment

Your Answer


