I want to use C# to access my own apex rest service in a managed package. I can easily do it in IOS and Android. But in C# I wrote code below, it can not work.

string restQuery = App._instanceUrl + @"/services/apexrest/namespace/myservice";  
string requestMessage = "Account,Contact";  

HttpContent content = new StringContent(Convert.ToBase64String(Encoding.UTF8.GetBytes(requestMessage)));

HttpClient queryClient = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, restQuery);  
request.Headers.Add("Authorization", "OAuth " + App._accessToken);  
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
request.Content = content;  

HttpResponseMessage response = await queryClient.SendAsync(request);  
string result = response.Content.ReadAsStringAsync().Result;    

What's wrong with my code?

  • What is the error you are receiving ? – Mihai Neagoe Jan 12 '15 at 9:40
  • There is only 401 error received. – BruceYue Jan 12 '15 at 10:01

You may want to look at the Force.com Toolkit for .NET as a reference. There are a couple of tutorials which may be useful as well.

Working with Custom SOAP and REST Services in .NET Applications

Consuming Force.com SOAP and REST Web Services from .NET Applications

If you are running into a 401, there's a good chance that your access token has expired. Refresh your access token and try again

  • Yes, I checked Force.com Toolkit for .NET,It seems only standard GRUD operation, don't have a custom apex rest call example. In other two links I found the demo code is the same with mine. But why my code can't work? – BruceYue Jan 12 '15 at 10:07
  • Updated answer, you should check for access token validity – Gaurav Kheterpal Jan 12 '15 at 10:40
  • Your authorization header looks different than mine. My header appears in curl as: "Authorization: Bearer 00DM0000001dSYq!ARAAQF1xqLq6o8cNEJ7VHxqaaRkNQlJoApQFuMbkpBLcPkC4szoxo7m6oMomen2TRIfqF20kErjhYy9ew5Aq1gc4Y8goaXUo". Note the "Bearer" before the token. – tggagne Jan 12 '15 at 17:00
  • I have tried to use "Bearer", but it still can't work. – BruceYue Jan 13 '15 at 1:24

I have solved this issue, The request param must be a json format.

//joining together the json format string sample:"{"key":"valus"}";
string requestMessage = "{\"jsonData\":\"" + Convert.ToBase64String(Encoding.UTF8.GetBytes("Account,Contact")) + "\"}";

HttpContent content = new StringContent(requestMessage, Encoding.UTF8, "application/json");

//create url using package name in URL
string restQuery = App._instanceUrl +@"/services/apexrest/namespace/MyService";

//create request message associated with POST verb
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, restQuery);

//return JSON to the caller
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

//add token to header
request.Headers.Add("Authorization", "Bearer " + App._accessToken);

//add content to HttpRequestMessage;
request.Content = content;

HttpClient putClient = new HttpClient();

//call endpoint async
HttpResponseMessage response = await putClient.SendAsync(request);

string result = response.Content.ReadAsStringAsync().Result;

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.