skip to Main Content

I am trying to get data from Baselinker.
But I am getting an error

Newtonsoft.Json.JsonReaderException: ‘Unexpected character encountered while parsing value: S. Path ”, line 0, position 0.’

The same in WPF

private async void Button_Clicked(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.baselinker.com/connector.php"))
                {
                    request.Headers.TryAddWithoutValidation("X-BLToken", "AAA-BBB-CCC");

                    request.Content = new StringContent("method=getOrders&parameters=%7B%22date_from%22%3A+1407341754%7D");
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

                    var response = await httpClient.SendAsync(request);
                    var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(Convert.ToString(response));
                    labelek.Text = oMycustomclassname;
                }
            }
        }

Help

2

Answers


  1. Convert.ToString(response) isn’t the right way to get the body of an HTTP response as a string. That’s likely to return "System.Net.Http.HttpResponseMessage" which clearly isn’t JSON.

    I suspect you want:

    var response = await httpClient.SendAsync(request);
    string content = await response.Content.ReadAsStringAsync();
    var deserialized = JsonConvert.DeserializeObject<dynamic>(json);
    
    Login or Signup to reply.
  2. An alternative:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.GetAsync("").Result;
    if (response.IsSuccessStatusCode)
       {
         string dataobject = response.Content.ReadAsStringAsync().Result;
         root = JsonConvert.DeserializeObject<Root>((string)dataobject);
         lista = root.Fundamentos;
       } ...
                
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search