.NET example of using the new API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • betdynamics
    Junior Member
    • Sep 2010
    • 534

    #1

    .NET example of using the new API

    Does anyone have a short example of how to use the new API using C# and .NET?

    I have tried to produce something but it just comes back with a "Bad Request" message whenever I make a call to the API.

    I want to try a working piece of code to check whether my Application Key is working or not.
  • betdynamics
    Junior Member
    • Sep 2010
    • 534

    #2
    It turns out that my appkey wasn't attached to the new API correctly.

    All sorted now - thanks to Neil at BDP Support.

    Comment

    • Contrarian2
      Junior Member
      • Jun 2012
      • 9

      #3
      betdynamics,

      Could you post your code example, please?

      I can't work out how to send a JSON request using C#.

      Thanks.

      Comment

      • betdynamics
        Junior Member
        • Sep 2010
        • 534

        #4
        I'm using the JSON.NET library to do it.

        Comment

        • Lopiner
          Junior Member
          • Feb 2009
          • 117

          #5
          Hi to all

          Im trying to make a request to the API but im getting "Bad Request" from the server in response. I don't know what am i doing wrong. Any help is much appreciated.

          This is my request:

          Code:
          Imports System.Text
          Imports Newtonsoft.Json
          Imports System.IO
          
          Module Requests
              Public Sub GetAllMarkets()
                  Dim URL As String
                  URL = APIEndPoint & ListEventTypes
                  Dim Request As Net.HttpWebRequest = Net.WebRequest.Create(URL)
                  
                  'URL = "https://beta-api.betfair.com/rest/v1.0/" & "listEventTypes/"
                  
                  Request.Accept = "*/*"
                  Request.AllowAutoRedirect = True
                  Request.Timeout = RequestTimeOut
                  Request.Method = "POST"
                  Request.KeepAlive = True
                  
                  Request.Headers.Add("X-Application", ApplicationKey) 'My API Key here
                  Request.Headers.Add("X-Authentication", SessionToken) 'A session token retrieved from the other API
                  Request.ContentType = "application/json"
          
                  Dim PostData As String
                  PostData = "{""filter"":{ }}"
                  Dim PostDataBytes As Byte() = Encoding.UTF8.GetBytes(PostData)
                  Request.ContentLength = PostDataBytes.Length
          
                  Dim PostStream As IO.Stream = Request.GetRequestStream()
                  PostStream.Write(PostDataBytes, 0, PostDataBytes.Length)
                  PostStream.Close()
          
                  Dim ResponseData As String
                  Dim Response As Net.HttpWebResponse = Request.GetResponse()
                  If Response.StatusCode = Net.HttpStatusCode.OK Then
                      Dim ResponseStream As IO.StreamReader = New IO.StreamReader(Response.GetResponseStream())
                      ResponseData = ResponseStream.ReadToEnd()
                  End If
                  Response.Close()
          
              End Sub
          End Module
          Thanks
          fooledbyabet.com

          Comment

          • gus
            Senior Member
            • Jan 2009
            • 134

            #6
            Dunno much (or indeed anything) about .NET, but your query doesn't look right to me.

            I think it should look something like this:

            {"filter":{"eventTypeIds":[]}}

            note the empty square brackets for an empty array

            rather than your:

            "{""filter"":{ }}"



            Anyway, that's what mine looks like, using REST from a Java app, and it works.

            Comment

            • Lopiner
              Junior Member
              • Feb 2009
              • 117

              #7
              Thanks gus

              I was following an example from the documentation. I have changed to your suggestion of sending an empty array but unfortunately im still getting a "Bad Request" response.

              I was looking for some way of analyzing the header and body of my request but i haven't found any way.

              I cant understand what am i doing wrong. Does anyone have an example in .net of a request to the API that is willing to share? Thanks
              fooledbyabet.com

              Comment

              • Lopiner
                Junior Member
                • Feb 2009
                • 117

                #8
                Maybe someone from the BDP team could post an example if they find the time. I think it would be useful for more users (the VB.NET topic is the most active in here)

                Thanks
                fooledbyabet.com

                Comment

                • PT_PT
                  Junior Member
                  • Nov 2012
                  • 17

                  #9
                  Originally posted by Lopiner View Post
                  Thanks gus

                  I was following an example from the documentation. I have changed to your suggestion of sending an empty array but unfortunately im still getting a "Bad Request" response.

                  I was looking for some way of analyzing the header and body of my request but i haven't found any way.

                  I cant understand what am i doing wrong. Does anyone have an example in .net of a request to the API that is willing to share? Thanks
                  Have you tried wireshark?

                  Comment

                  • betdynamics
                    Junior Member
                    • Sep 2010
                    • 534

                    #10
                    Do other requests work (so you have confirmed that your API key is working)?
                    Last edited by betdynamics; 13-03-2013, 11:49 AM.

                    Comment

                    • aubergine
                      Junior Member
                      • Jan 2010
                      • 19

                      #11
                      Fiddler is the tool you want for inspecting headers, POST data etc. This will also give you the JSON bad request response (assuming you're hitting the API), post it here and I'll have a look if you like.

                      This is what a JSONRPC request should look like:

                      {"id":0,"jsonrpc":"2.0","method":"SportsAPING/v1.0/listCompetitions","params":{"filter":{"eventTypeId s":[1]}}}

                      All you need to do is create a .NET class that serializes to that form (using JSON.NET for example), something like:

                      Code:
                      public class JsonRpcRequest
                          {
                              private IOperation _operation { get; set; }
                      
                              public int Id { get; set; }
                              public string JsonRpc { get { return "2.0"; } }
                              public string Method { get { return "SportsAPING/v1.0/" + _operation.Method; } }
                              public IDictionary<string, object> Params { get { return _operation.Parameters; } }
                          }
                      Note: this is not a complete sample, I've got a more complicated inheritance setup, but should give you an idea. Plus I'm using RPC, not REST, but there's not much between them.

                      Then create a bog standard WebRequest, or use the new HttpClient which is pretty straightforward.

                      HTH

                      Comment

                      • Lopiner
                        Junior Member
                        • Feb 2009
                        • 117

                        #12
                        Thank you all. I haven't had the time to test the suggestions. Will do that and post the results if someone else find it useful.
                        fooledbyabet.com

                        Comment

                        • robne2000
                          Junior Member
                          • May 2013
                          • 19

                          #13
                          I've always been more interested in what I can use the API to do in terms of strategies, rather than coding. In the past I've managed to get by mainly by expanding on examples provided by others.

                          So just wondering if anyone has any working VB.NET examples they can share?

                          If not, I've made a little progress and will continue at the weekend, so I'll post it once I have something working.

                          Comment

                          • vic
                            Junior Member
                            • May 2009
                            • 33

                            #14
                            Originally posted by robne2000 View Post
                            I've always been more interested in what I can use the API to do in terms of strategies, rather than coding. In the past I've managed to get by mainly by expanding on examples provided by others.

                            So just wondering if anyone has any working VB.NET examples they can share?

                            If not, I've made a little progress and will continue at the weekend, so I'll post it once I have something working.
                            Robne2000
                            I too get by like you do but so far there is a lack of good examples in c#. For example what's all this application key thingy. Why can't we just log in. I suppose I'll be using my old code until we have better examples. Cheers

                            Comment

                            Working...
                            X