0

I have tried but dont know how to read data from API response. I was able to get 200 status code but I can't figure out how to get actualdata. I am trying to get data from Withings API (http://developer.withings.com/oauth2/#tag/measure%2Fpaths%2Fhttps%3A~1~1wbsapi.withings.net~1measure%3Faction%3Dgetmeas%2Fget)

Below response i got:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, OPTIONS Access-Control-Allow-Headers: Content-Type, * Date: Mon, 10 Jun 2019 11:51:03 GMT Server: Apache Content-Length: 65 Content-Type: text/plain; charset=UTF-8

The following is my code:

        string accessToken = "my_accessToken";
        HttpClient  client = new HttpClient();
        var url = "https://wbsapi.withings.net/measure?action=getmeas&meastype=1&category=1&startdate=737060&enddate=737179&offset=0";
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
        var response = await client.GetAsync(url);
        string content1 = await response.Content.ReadAsStringAsync();
        string content2 = response.Content.ReadAsStringAsync().Result;

Above i have added link for withings API. Is anyone guide how to get data from response body.

intspdev
  • 1
  • 3
  • With a content length of 65, thats not going to say a lot. Your content1 should contain the string returned.. – BugFinder Jun 10 '19 at 13:57
  • @BugFinder on content1 i got below message content1: {"status":2554,"error":"Not implemented: Invalid protocol TLSv1"} – intspdev Jun 10 '19 at 14:00
  • that is the response :) as I said its short. – BugFinder Jun 10 '19 at 14:07
  • @BugFinder If thats response then where is my data? and if its Error then what it syas? and what i need to do now? – intspdev Jun 10 '19 at 14:15
  • @intspdev you're not getting any data because there has been an error. The Withings API must have documentation which tells you what a status "2554" is, we can't tell you that but it looks like from the message it's something to do with an invalid protocol. – Chris Jun 10 '19 at 14:36
  • You could argue that it's wrong that the api is returning a 200 status code (and a content type of "text/plain"), but I think BugFinder is correct, and the body actually an error message – ste-fu Jun 10 '19 at 14:49
  • 2
    Assuming the API error is related to TLS1, you might want to try this `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;` before making the request. This should force you code into using a higher version of the protocol – ste-fu Jun 10 '19 at 14:53
  • The response is telling you you didnt send the question in the right format, eg it wanted something other than TLSv1. – BugFinder Jun 10 '19 at 14:54
  • Looking at the error it looks like the API doesn't accept TLSv1, see this [answer](https://stackoverflow.com/a/33091871/1124565) on how to change the protocol for `HttpClient` – amura.cxg Jun 10 '19 at 14:55
  • @ste-fu Thanks. it works. amura.cxg thank you as well for detail answer – intspdev Jun 11 '19 at 14:58

1 Answers1

0

Problem has been resolved now. The problem was API does not support TLS1 and as i set security protocol to Tls12 it works. Below is code which i used.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Thanks to all.

intspdev
  • 1
  • 3