2

I am struggling to find out why my query throws an exception when I do a specific query. Is there a way to see the raw response from Salesforce or .NET? Query string in itemQueryString is running correctly using workbench so I have ruled out query string problem.

Here is my code:

try{
    var results = forceClient.QueryAsync<JObject>(itemQueryString).Result;
    //do something...
} catch (Exception e) {
    System.Diagnostics.Trace.TraceError("Error while querying items " + e.ToString());
}

Here is the exception

"System.AggregateException: One or more errors occurred. ---> 
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while 
parsing value: <. Path '', line 0, position 0.\r\n   at 
Newtonsoft.Json.JsonTextReader.ParseValue()\r\n   at 
Newtonsoft.Json.JsonTextReader.Read()\r\n   at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.
ReadForType(JsonReadrr reader, JsonContract contract, Boolean 
hasConverter)\r\n   at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.
Deserialize(JsonReader reader, Type objectType, Boolean 
checkAdditionalContent)\r\n   at Newtonsoft.Json.JsonSerializer.
DeserializeInternal(JsonReader reader, Type objectType)\r\n   at 
Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, 
JsonSerializerSettings settings)\r\n   at 
Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, 
JsonSerializerSettings settings)\r\n   at 
Salesforce.Common.ServiceHttpClient.<HttpGetAsync>d__1`1.MoveNext()\r\n   --
- End of inner exception stack trace ---\r\n   at 
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean 
includeTaskCanceledExceptions)\r\n   at 
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, 
CancellationToken cancellationToken)\r\n   at 
System.Threading.Tasks.Task.Wait()\r\n   at 
ReportDataManager.PopulateItems(ItemQueryString) in 
ReportDataManager.cs:line 39\r\n---> (Inner Exception #0) 
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while 
parsing value: <. Path '', line 0, position 0.\r\n   at 
Newtonsoft.Json.JsonTextReader.ParseValue()\r\n   at 
Newtonsoft.Json.JsonTextReader.Read()\r\n   at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.
ReadForType(JsonReader reader, JsonContract contract, Boolean 
hasConverter)\r\n   at 
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.
Deserialize(JsonReader reader, Type objectType, Boolean 
checkAdditionalContent)\r\n   at 
Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type 
objectType)\r\n   at Newtonsoft.Json.JsonConvert.DeserializeObject(String 
value, Type type, JsonSerializerSettings settings)\r\n   at 
Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, 
JsonSerializerSettings settings)\r\n   at 
Salesforce.Common.ServiceHttpClient.<HttpGetAsync>d__1`1.MoveNext()<---\r\n"    

Looks like it's returning unexpected result so query result cannot be parsed but I don't know how to get the raw response so I can see what is wrong with the query.

I tried to use system.net trace according to this blog; however, my trace is encrypted I cannot see the response. https://blogs.msdn.microsoft.com/waws/2017/03/14/how-to-get-a-system-net-trace-of-your-c-web-job/

2

Lets go down the rabbit hole!

  1. ForceClient.QueryAsync
  2. JsonHttpClient.HttpGetAsync
  3. BaseHttpClient.HttpGetAsync

    protected async Task<string> HttpGetAsync(Uri uri)
    {
        var responseMessage = await HttpClient.GetAsync(uri).ConfigureAwait(false);
        // ...
    }    
    

If you were using the toolkit source directly rather than via nuget you could just grab the responseMessage.Content.ReadAsStringAsync() result as needed.

Otherwise your options are to:

  1. Log the HttpClient requests/responses, or
  2. Use a OS level tool to intercept the network traffic.
  • Thanks so much for the pointer. I used HttpClient instead of Forceclient to send the exact request. I was able to get HttpResponseMessage that contains the raw response. Finally I can see the error! – user2855938 Sep 7 '17 at 18:27

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.