2

I am attempting to pull an attachment and serve it up in an API. I am consistently getting a bad request error, And I cannot see to understand why this is not producing the byte stream I am looking for. Can someone take a look and offer insight?

A couple points: I am validating into SFDC without issue. I have tried to do this through an async query instead and have only gotten a link that does not help.

        var req = (HttpWebRequest)WebRequest.CreateHttp(Properties.Settings.Default.url3 + documentId + "/body ");
        req.Headers.Add("Authorization: Bearer " + Properties.Settings.Default.token);
        req.Method = "GET";

        WebResponse resp;
        byte[] doc = null;
        String content;

        Console.Write(req);

        resp = req.GetResponse();
        StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
        content = sr.ReadToEnd();
        doc = Convert.FromBase64String(content);

        return doc;
  • What does Properties.Settings.Default.url3 look like? Is it correctly set to the serverURL that comes back from the authentication steps? – Daniel Ballinger Feb 27 '17 at 18:54
1

The bad request error is likely caused by having the incorrect pod/instance in the request URL.

Also, you don't need to base64 decode the response. It will be the raw binary form. See Get Attachment Content from a Record

        var auth = new AuthenticationClient();
        await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password);

        string url = auth.InstanceUrl + "/services/data/"+ auth.ApiVersion + "/sobjects/Attachment/00P7000000rfpYPEAY/body";

        var req = (HttpWebRequest)WebRequest.CreateHttp(url);
        req.Headers.Add("Authorization: Bearer " + auth.AccessToken);
        req.Method = "GET";

        WebResponse resp = req.GetResponse();
        byte[] doc = null;
        using (MemoryStream ms = new MemoryStream())
        {
            resp.GetResponseStream().CopyTo(ms);
            doc = ms.ToArray();
        }

        File.WriteAllBytes("attachment.png", doc);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.