문제

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);
        req.ContentType = "application/x-www-form-urlencoded";
        string par = 
               "fileName=" + fileName +
            "&feedItemFileUpload="                
           + @"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);
        dataStream.Close();

        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.

도움이 되었습니까?

해결책

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top