dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Http Request with Custom Headers but response is 400 (Bad request)??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    28

    Resolved [RESOLVED] Http Request with Custom Headers but response is 400 (Bad request)??

    This is my code which i am using to send Http Request with Custom Headers. But server response is 400 (Bad request).

    Code:
    Try
                Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"), HttpWebRequest)
    
                Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
    
                myHttpWebRequest.Method = "GET"
    
                myHttpWebRequest.Headers("Connection") = "keep-alive"
    
                myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
    
                myHttpWebRequest.Headers("X-Authentication") = "Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM="
    
                myHttpWebRequest.Headers("Accept") = "application/json"
    
                myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
    
                myHttpWebRequest.GetResponse()
    
                Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
    
                Dim streamRead As New StreamReader(streamResponse)
    
                Dim readBuff(256) As [Char]
    
                Dim count As Integer = streamRead.Read(readBuff, 0, 256)
    
                While count > 0
                    Dim outputData As New [String](readBuff, 0, count)
                    RichTextBox1.Text = outputData
                    count = streamRead.Read(readBuff, 0, 256)
                End While
    
                streamResponse.Close()
                streamRead.Close()
                myHttpWebResponse.Close()
    
            Catch ex As Exception
    
                MsgBox(ex.Message)
                
            End Try
    This is required parameters for Http Request.

    Example Request
    Code:
     
    GET https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json
    Connection: keep-alive
    X-Application: <AppKey>
    X-Authentication: <SessionToken>
    Accept: application/json
    Accept-Encoding: gzip,deflate
    I test with Betfair Api Demo Tool to call Api and i captured their http header using Live HTTP Headers Addon of Chrome and this was output.

    Code:
    POST /api.betfair.com/exchange/betting/json-rpc/v1 HTTP/1.1
    Host: developers.betfair.com:443
    Accept: */*
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    Content-Type: application/json
    Cookie: wsid=0476c9a1-1701-11e8-8a4b-fa163e5001b9; vid=941b3fcd-c91e-4175-b614-3e1cfe2ebd4e; _ga=GA1.2.1638576003.1519215392; _gid=GA1.2.628600155.1519215392; ssoid=Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM=; loggedIn=true; mgr=y; lka=1519215409118; bfsd=ts=1519215409118|st=reg; userhistory=15191043561519215412645|1|Y|210218|210218|home|Y; betexPtk=betexCurrency%3DGBP%7EbetexTimeZone%3DEurope%2FLondon%7EbetexRegion%3DGBR%7EbetexLocale%3Den; betexPtkSess=betexRegionSessionCookie%3DGBR%7EbetexCurrencySessionCookie%3DGBP%7EbetexTimeZoneSessionCookie%3DEurope%2FLondon%7EbetexLocaleSessionCookie%3Den%7EbetexSkin%3Dstandard%7EbetexBrand%3Dbetfair; gtmfl=qD4Oj; cto_lwid=7a955e73-8e43-4310-92ba-c103ccdacfdf; appkey=WcnmFApc9e8M5OA0
    Origin: https://developers.betfair.com
    Referer: https://developers.betfair.com/visualisers/api-ng-sports-operations/
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
    X-Application: WcnmFApc9e8M5OA0
    X-Authentication: Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM=
    X-Requested-With: XMLHttpRequest
    
    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Connection: close
    Content-Encoding: gzip
    Content-Length: 542
    Content-Type: application/json
    Date: Wed, 21 Feb 2018 14:57:55 GMT
    Vary: Accept-Encoding,User-Agent
    Attached Images Attached Images  

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,162

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    There's quite a few problems with your code.

    To begin with, you are requesting the response twice: once before you set the request headers, and then again after they are set.
    Code:
    Try
                Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"), HttpWebRequest)
    
                Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
    
                myHttpWebRequest.Method = "GET"
    
                myHttpWebRequest.Headers("Connection") = "keep-alive"
    
                myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
    
                myHttpWebRequest.Headers("X-Authentication") = "Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM="
    
                myHttpWebRequest.Headers("Accept") = "application/json"
    
                myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
    
                myHttpWebRequest.GetResponse()
    Only request the response the one time, after the headers have been set.


    Next, the way you are setting up your headers won't work for some of the protected headers:
    Code:
    myHttpWebRequest.Headers("Connection") = "keep-alive"
    myHttpWebRequest.Headers("Accept") = "application/json"
    should be:
    Code:
    myHttpWebRequest.KeepAlive = True
    myHttpWebRequest.Accept = "application/json"

    Finally, you've told the server that your app will accept its reply in a compressed form (myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"), so you need to decompress the data from the response stream. You can use the System.IO.Compression.GZipStream Class to perform the decompression on the fly.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    28

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    Thanx for your reply. I edited the code like you suggested now their is an error when execute this code.
    "Value cannot be null. Parameter name: destination"

    Here is my edited code now.

    Code:
    Try
                Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"), HttpWebRequest)
    
                Dim myHttpWebResponse As HttpWebResponse
    
                myHttpWebRequest.Method = "GET"
    
                myHttpWebRequest.KeepAlive = True
    
                myHttpWebRequest.Accept = "application/json"
    
                myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
    
                myHttpWebRequest.Headers("X-Authentication") = "Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM="
    
                myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
    
                myHttpWebResponse = myHttpWebRequest.GetResponse
    
                Dim streamResponse As Stream = Nothing
    
                Using streamCompressedResponse As New GZipStream(myHttpWebResponse.GetResponseStream(), CompressionMode.Decompress)
    
                    streamCompressedResponse.CopyTo(streamResponse)
    
                End Using
    
    
                Dim streamRead As New StreamReader(streamResponse)
    
                Dim readBuff(256) As [Char]
    
                Dim count As Integer = streamRead.Read(readBuff, 0, 256)
    
                While count > 0
                    Dim outputData As New [String](readBuff, 0, count)
                    RichTextBox1.Text = outputData
                    count = streamRead.Read(readBuff, 0, 256)
                End While
    
                streamResponse.Close()
                streamRead.Close()
                myHttpWebResponse.Close()
    
            Catch ex As Exception
    
                MsgBox(ex.Message)
    
            End Try

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    28

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    I figured that reason of error is, streamResponse is null when passing as argument to streamCompressedResponse.CopyTo method.
    So what is proper and efficient way to get that http response stream, decompress it using GZipStream and show its JSON output in RichTextBox?

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,162

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    Quote Originally Posted by Terrybogard911 View Post
    I figured that reason of error is, streamResponse is null when passing as argument to streamCompressedResponse.CopyTo method.
    So what is proper and efficient way to get that http response stream, decompress it using GZipStream and show its JSON output in RichTextBox?
    Yep, good catch.

    I'd probably write it something like this:
    Code:
    myHttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
    
    
    Using deCompressedStream As New GZipStream(myHttpWebResponse.GetResponseStream(), CompressionMode.Decompress)
        Using streamRead As New StreamReader(deCompressedStream)
            RichTextBox1.Text = streamRead.ReadToEnd()
        End Using
    End Using
    
    myHttpWebResponse.Close()

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    28

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    Thanx. I got it working because of your help. Really Appreciated.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,124

    Re: Http Request with Custom Headers but response is 400 (Bad request)??

    As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width