GDax GET API always returns 400 error

I am trying to get an order book from GDAX (link to call documentation), but when I do this from a C # executable, I always get Error 400 - Bad request.

When you take the actual URL and paste it into your browser, it works great.

String URL = "https://api.gdax.com/products/BTC-USD/book?level=2";
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
+4
source share
2 answers

Actual problem with calling the API, the API expects the user agent string when executing the request: The following is the code in working condition:

        try
        {

            String URL = "http://api.gdax.com/products/BTC-USD/book?level=2";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);                
            request.UserAgent = ".NET Framework Test Client";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            var encoding = ASCIIEncoding.ASCII;
            using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
            {
                string responseText = reader.ReadToEnd();
            }

        }
        catch(WebException ex)
        {
            HttpWebResponse xyz = ex.Response as HttpWebResponse;
            var encoding = ASCIIEncoding.ASCII;
            using (var reader = new System.IO.StreamReader(xyz.GetResponseStream(), encoding))
            {
                string responseText = reader.ReadToEnd();
            }
        }

ProtocolError , , , , , . catch ex.Response( HttpWebResponse) , API , . "{" ":" User-Agent ".}"

, , ,

. WebRequest HttpWebRequest, , HTTP, , , UserAgent, WebRequest.

+8

, Google - https.

0

Source: https://habr.com/ru/post/1680688/


All Articles