Announcement

Collapse
No announcement yet.

Issue with connection for Book Prices

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue with connection for Book Prices

    Hi All,

    Am new to development for Betfair and am trying to create a list of horse racing UK races and runners with their Back and Lay prices displayed. I can get the course names and and runners etc...but am having an issue when trying to populate the prices...I keep getting error message "The remote server returned an error (400) Bad Request

    I checked the URL and this seems to be correct -- https://api.betfair.com/exchange/betting/json-rpc/v1

    Below is the code where I appear to be hitting the issue....any help would be greatly appreciated (I have removed my developer keys from the code below Request.Headers.Add

    ------------------------------------------

    Private Function SendSportsReq(ByVal jsonString As String)
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    Dim request As HttpWebRequest =
    WebRequest.Create _
    ("https://api.betfair.com/exchange/betting/json-rpc/v1")

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(jsonString)

    Dim responseFromServer As String = ""

    Try
    request.Method = "POST"
    request.ContentType = "application/json"
    request.ContentLength = byteArray.Length
    request.Headers.Add("X-Application:

    request.Headers.Add("X-Authentication: " & ssoid)
    request.AutomaticDecompression =
    DecompressionMethods.GZip Or DecompressionMethods.Deflate
    request.ServicePoint.Expect100Continue = False
    request.Timeout = 2000

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)

    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()

    Dim reader As New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()
    'might need to change this as changed from form1
    'frmDiagnostics.Print(responseFromServer)

    reader.Dispose()
    dataStream.Dispose()
    response.Dispose()
    'might need to change this as changed from form1
    Catch ex As WebException 'Exception
    frmDiagnostics.Print("SendSportsReq Error: " & ex.Message)
    End Try

    Return responseFromServer

    End Function
    ----------
    Thanks for any assistance

    Cheers

    Aidan

  • #2
    Hi

    Can you post the json string that you are sending down the wire from which you are getting the 400 response? Should be easier to figure out what the issue is.

    Comment


    • #3
      Thanks Geoff

      The json string is
      "[{""jsonprc"":""2.0"",""method"":""SportsAPING/v1.0/listMarketBook"",""params"":{""marketIds"":[""1.187065287"",""1.187066829"",""1.187065290"","" 1.187066830"",""1.187065293"",""1.187066831"",""1. 187065296"",""1.187066832"",""1.187065299""],""priceProjection"":{""priceData"":[""EX_BEST_OFFERS"",""EX_TRADED""]},""orderProjection"":""ALL""},""id"":1}]"

      Also I notice when debugging 2 errors in datastream.
      'DirectCast(dataStream, System.Net.ConnectStream).Length' threw an exception of type 'System.NotSupportedException'
      'DirectCast(dataStream, System.Net.ConnectStream).Position' threw an exception of type 'System.NotSupportedException'

      See attached...thanks again for any help.

      Cheers

      Aidan
      Attached Files

      Comment


      • #4
        hi

        Sorry, this isnt going to help you with the problem, I cleaned your json request up so I could compare it more easily with an example from the spec.
        See below. (The CODE tags on this site dont preserve spacing but hopefully is still readable)


        Code:
        ------------------------------------------
        Yours
        
        [
        {
        "jsonrpc": "2.0",
        "method": "SportsAPING/v1.0/listMarketBook",
        "params": {
        "marketIds": [
        "1.183717115",
        "1.183709733",
        "1.183709735"
        ],
        "priceProjection": {
        "priceData": [
        "EX_BEST_OFFERS"
        ],
        "virtualise": "true"
        }
        },
        "id": 1
        }
        ]
        
        ----------------------------------------------
        
        From Spec
        [
        {
        "jsonrpc": "2.0",
        "method": "SportsAPING/v1.0/listMarketBook",
        "params": {
        "marketIds": [
        "1.127771425"
        ],
        "priceProjection": {
        "priceData": [
        "EX_BEST_OFFERS",
        "EX_TRADED"
        ],
        "virtualise": "true"
        }
        },
        "id": 1
        }
        ]
        Yours looks fine in comparison to the spec. document example. Do you get any json response at all ? If so maybe post that here too, it might help figure it out.

        Comment


        • #5
          Hi Geoff,

          Thanks again for the time...the response I get is "The remote server returned an error (400) Bad Request". Would it help to zip the solution and add it here ..It is relatively small only a couple of classes.

          Appreciate the time on this.

          Cheers

          aidan

          Comment


          • #6
            hi

            Been a long time since I tried messing with VB.net I dont mind giving it a quick try for you if you would like to post it here. No promises I can get it to run though
            Dont forget to remove your Betfair key though if you have purchased one ?

            Comment


            • #7
              Thanks a million Geoff...I am attaching zip file with the Betfair key.....if you can get anywhere with it, excellent...if not , no worries...
              Attached Files

              Comment


              • #8
                Hi

                I got it to run but get a NO_APP_KEY response to the ListMarketCatalog request. It looks like you do the App key thing totally different from how i do it, so I havent figured where you even specify the App_Key in your code. So I am snookered on that for the moment, any hints on that ?

                Update:
                Ah cancel that question, I had a bit of a hack at your code and can now get ListMarketCatalog to work. Was bugging me having to type user name and password via the Betfair webpage so I cut that out and replaced it with some code to save me that tedious typing everytime I run it. I should be able to have a look at the prices problem now at least

                Last edited by geoffw123; 01-09-2021, 11:18 PM.

                Comment


                • #9
                  Hi Aidan

                  Finally spotted it, took me longer to spot than I am willing to admit The really strange thing is that when I cleaned and pasted your json string you posted earlier on, it somehow corrected the error, so when I was studying the differences between yours and the spec, it looked fine.

                  It is just a typo in your listMarketBook code the variable name should be jsonrpc not jsonprc. You got it correct in the listmarketcatalog command so that worked fine.

                  P.S1
                  I had a bit of trouble with your marketStartTime code and it looked pretty nasty to get the from and to times
                  I changed it to this, looks nicer I think (you can check if it works as intended)
                  Code:
                  marketStartTime.from = Format(Date.Now.ToLocalTime(), "yyyy-MM-ddTHH:mm:ssZ")
                  marketStartTime.to = Format(Date.Now.AddHours(20).ToLocalTime(), "yyyy-MM-ddTHH:mm:ssZ")
                  PS2
                  For your original faulty code, you were checking in an infinite do while loop that the json response was not "". As it was failing, you were spamming Betfair's server with the continually failing request. Betfair wont like that, you will end up on the naughty step I hacked out that do while loop on fail

                  Comment


                  • #10
                    Hi Geoff,

                    That is working there now....Thank you so much for spending the time on this. Owe you one!

                    Best Regards

                    Aidan

                    Comment

                    Working...
                    X