I want to send http request using same headers. I have done most things but couldn't figure out how to include filter. Here is my code in VB.NET.
Code:
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"), HttpWebRequest)
myHttpWebRequest.Method = "GET"
myHttpWebRequest.KeepAlive = True
myHttpWebRequest.Accept = "application/json"
myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
myHttpWebRequest.Headers("X-Authentication") = "GLksOhuoCPQTD6RK0TrlY/R+uKf4Zx9jOa5BUsuBwbg="
myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
Using responseStream = New GZipStream(myHttpWebResponse.GetResponseStream(), CompressionMode.Decompress)
Using reader As New StreamReader(responseStream, Encoding.Default)
RichTextBox1.Text = reader.ReadToEnd()
End Using
End Using
The POST data contains the request parameters. For listEventTypes, the only required parameter is a filter to select markets. You can pass an empty filter to select all markets, in which case listEventTypes returns the EventTypes associated with all available markets.
Your current code is sending a request to a different endpoint using the GET method.
Then you have to send the JSON string '{ "filter" : { } }' to the server as part of the HttpWebRequest .
To do this, you would typically
Convert the string to a Byte Array using a suitable Text Encoding.
Set the request's ContentLength Header to the array length.
Set the request's ContentType Header to "application/json".
Write the contents of the Byte Array to the HttpWebRequest's request stream.
So your code should be something like:
Code:
Dim url As String = "https://api.betfair.com/exchange/betting/rest/v1.0/listEventTypes/"
ServicePointManager.Expect100Continue = False
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
myHttpWebRequest.Method = "POST"
myHttpWebRequest.KeepAlive = True
myHttpWebRequest.Accept = "application/json"
myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
myHttpWebRequest.Headers("X-Authentication") = "GLksOhuoCPQTD6RK0TrlY/R+uKf4Zx9jOa5BUsuBwbg=n"
myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
Dim postData As String = "{ ""filter"" : { } }"
Dim dataBytes() As Byte = Encoding.UTF8.GetBytes(postData)
myHttpWebRequest.ContentLength = dataBytes.Length
myHttpWebRequest.ContentType = "application/json"
Using reqStream As Stream = myHttpWebRequest.GetRequestStream()
reqStream.Write(dataBytes, 0, dataBytes.Length)
End Using
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
Using responseStream = New GZipStream(myHttpWebResponse.GetResponseStream(), CompressionMode.Decompress)
Using reader As New StreamReader(responseStream, Encoding.Default)
RichTextBox1.Text = reader.ReadToEnd()
End Using
End Using
Please note: When using .NET you set the "Expect -100Continue" property to false. This property sits in the ServicePointManager class
and I've included the necessary statement in the code above (ServicePointManager.Expect100Continue = False)
Also note that the example code is passing an empty filter, so you'll get back all the available EventTypes in the response.
One final point: why are you using Encoding.Default? Encoding.Default is almost always the wrong choice of encoding because it varies from PC to PC, based on user preference settings. Did UTF8 not work properly for you?
Actually i have found code on internet and with help of you and some others, changed it a bit. Didn't know what is difference between Encoding.Default and Encoding.UTF8 though i changed it to UTF8 as you said and your above code worked perfectly. I got response something like below.
Another quick question, why sending http request to betfair Api doesn't work without vpn? I am forced to use vpn otherwise it returns error. I live in pakistan and i have used many application who display betfair odds and don't need vpn.
Didn't know what is difference between Encoding.Default and Encoding.UTF8 though i changed it to UTF8 as you said and your above code worked perfectly.
The data you send to and receive from Betfair is a sequence of byte values. The Encoding determines how text is converted to/from those byte values. If your App and the Betfair API are using different character encodings, any text communicated can become garbled.
As I mentioned earlier, Encoding.Default varies from PC to PC (and won't be UTF-8), so there's a good chance it will be different to the Encoding being used by the Betfair API.
Ideally, you should specify the character encoding in the HTTP request headers, and Betfair should specify its character encoding in the response Headers.
If the character encoding isn't specified, then my guess would be that UTF-8 is assumed, but whatever is used, Encoding.Default is guaranteed to fail at some point depending on the characters in the text you are sending/receiving (although you are less likely to run into problems if you are only using characters from the English alphabet; it's the accented or non English characters that would cause you grief).
All this should be outlined in the documentation, but I haven't been able to find it yet.
With regards to the documentation, most of the code examples given are for the json-RPC API (you are using the json-REST API), and those code examples do indeed contain headers that specify text encodings. I have no idea if those headers are used by the REST API though.
Originally Posted by Terrybogard911
So what should be my next step if i want to get specific event markets like cricket. Should it be put
Dim postData As String = "{ ""filter"" : {cricket} }" OR Dim postData As String = "{ ""filter"" : {4} }" and POST it to same Api endpoint?
The first thing to do would be to try it...and consult the documentation when it doesn't work.
Again, I haven't spotted anything in the documentation that shows how to set up a filter for the REST API. However there are plenty of examples given for the RPC endpoints such as for Request a List of Events for an Event Type:
which is filtering on 'Soccer' events (eventType Id = 1) between a certain time range.
So for Cricket (eventType Id = 4) without also filtering by time range, I'd guess your filter would be:
"{ ""filter"": { ""eventTypeIds"": [""4""] } }"
or maybe:
"{ ""filter"": { ""textQuery"": ""Cricket"" } }"
But that's only a guess, because I don't have a valid session token to test with.
So try using Dim postData As String = "{ ""filter"": { ""eventTypeIds"": [""4""] } }" in the code from post#2 above.
BUT you'd probably want to use a different endpoint with the filter to get specific information about that specific event type (Cricket in your example).
So you might pass the Cricket EventTypeId as a filter to a call to the ListEvents endpoint to get just the information about all cricket events. Then use some of that information to filter a call to the listMarketCatalogue endpoint to get specific market information for a specific Cricket event, and so on.
Try following through theExample Requests to get an idea of the process.
Another quick question, why sending http request to betfair Api doesn't work without vpn? I am forced to use vpn otherwise it returns error. I live in pakistan and i have used many application who display betfair odds and don't need vpn.
Please note: Your Betfair account must be verified before your application will be considered. We do not accept Software Vendor licence applications from India, Pakistan, Bangladesh, Sri Lanka or UAE.
so maybe they are also imposing restrictions for the APIs you are using?
Adding those brackets [ ] helped and its now working and i am now getting specific sports markets.
Code:
"{ ""filter"": { ""eventTypeIds"": [""1""] } }"
Don't know. But in the blurb for their vendor program they state:
Please note: Your Betfair account must be verified before your application will be considered. We do not accept Software Vendor licence applications from India, Pakistan, Bangladesh, Sri Lanka or UAE.
so maybe they are also imposing restrictions for the APIs you are using?
Maybe but i am still using those betting bots here in pakistan which provide betfair odds in realtime. Watch attached screenshot.
Last edited by Terrybogard911; Mar 4th, 2018 at 09:08 AM.