3

I have below code to fetch account details from salesforce using REST API. But i am getting bad request status in the response(400). Could you please help me on this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Specialized;
using System.Net;

public class DemoController
    {

    static void Main(string[] args)
        {
            try
            {
                string endPOINTURL = String.Format("https://chaithraservicecloudsso-dev-ed.my.salesforce.com/services/data/v37.0/sObjects/Account");

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(endPOINTURL);

                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "application/Json";
                //httpWebRequest.Headers.Add("Content-Type", "application/json");
                httpWebRequest.Headers.Add("Authorization", "Bearer 00D280000017weU!ARgA_DoNotPublishYourFullSessionId__L7CvP7t9DxTn3dF3MBxy");

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Console.WriteLine("Response" + httpResponse);
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
            }
        }
}

1 Answer 1

2

The URL is case sensitive.

Note the capital you have in your URL:

/services/data/v37.0/sObjects/Account

Instead you want:

/services/data/v39.0/sobjects/Account

If in doubt, Workbench is a great tool to test the REST API calls out from.


Also, you need to specify the subdomain on the endpoint.

You endpoint should change from:

https:// test. salesforce.com/services/data/v37.0/sObjects/Account

To something like:

https:// cs14. salesforce.com/services/data/v37.0/sobjects/Account

The specific subdomain you require will depend on which pod/instance you org is hosted on. Typically when you establish the SessionID you will also get the ServerURL back that needs to be used for subsequent requests.


I used you code verbatim from the question from a C# console application. It worked fine after I corrected the endpoint URL as per the case sensitivity not above.

    static void Main(string[] args)
    {
        string endPOINTURL = String.Format("https://ap2.salesforce.com/services/data/v37.0/sobjects/Account");

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(endPOINTURL);

        httpWebRequest.Method = "GET";
        httpWebRequest.Accept = "application/Json";
        //httpWebRequest.Headers.Add("Content-Type", "application/json");
        httpWebRequest.Headers.Add("Authorization", "Bearer 00D280000017weU!ARgA_DoNotPublishYourFullSessionId_jzISX7MBtOriTqkF8Hp3LzZh2OOh_L7CvP7t9DxTn3dF3MBxy");

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        Console.WriteLine("Response" + httpResponse);
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }

enter image description here


As per the comments, the last part of the puzzle would be ensuring you have TLS 1.2 support from both the .NET framework and the operating system. See Salesforce disabling TLS 1.0

10
  • Nope..I tried..it did not work.. Mar 30, 2017 at 2:48
  • @ChaithraKN You also need to change the test. subdomain to the correct value for you pod/instance. If it is a sandbox it will be something like: https://cs15.salesforce.com/ Mar 30, 2017 at 3:36
  • Yes.. I have tried that too.. It is not working Mar 30, 2017 at 3:41
  • @ChaithraKN Can you update your question to append your current call? There might be something more subtle happening. Also, did you get it to work in Workbench? When you do, you can pull the raw HTTP GET request from the browser developer tools. Mar 30, 2017 at 3:45
  • 1
    @ChaithraKN TLS support is a whole other question. Best to pursue that separately. Mar 30, 2017 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .