REST endpoint - Header name invalid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PaulD
    Junior Member
    • Nov 2019
    • 4

    #1

    REST endpoint - Header name invalid

    Hello,

    After logging in and get account information, account funds and current orders without error, I am trying to use the REST endpoint https://api.betfair.com/exchange/bet...istEventTypes/ with a WebClient / C# and pass an empty {"filter" : { } } post data.

    What I am getting is always an exception with a detail: "Header name is invalid"

    The headers I am sending are:

    request.Headers.Add("Accept", "application/json");
    request.Headers.Add("Content-Type", "text/json");
    request.Headers.Add("X-Authentication", <token>);
    request.Headers.Add("X-Application", <api-key>);

    I tried several variations of header requests like content-type: application/json and not sending some of the others but the exception is always the same.

    I am thinking that the POST I have to do needs something different because what it works so far didn't have any data sent.

    All the samples I could find in the documentation and here in the forum are using the NG endpoint

    Any clues?
  • WTPooh
    Member
    • May 2012
    • 88

    #2
    request.ContentType = "application/json";
    request.Accept = "application/json";
    request.Headers.Add("X-Authentication", <token>);
    request.Headers.Add("X-Application", <api-key>);

    Comment

    • PaulD
      Junior Member
      • Nov 2019
      • 4

      #3
      There are no properties 'ContentType' and 'Accept' in WebClient

      https://docs.microsoft.com/en-us/dot...tframework-4.8

      Changing to:

      request.Headers.Add(HttpRequestHeader.Accept, "application/json");
      request.Headers.Add(HttpRequestHeader.ContentType, "application/json");

      changed nothing
      Last edited by PaulD; 15-11-2019, 09:23 PM.

      Comment

      • jabe
        Senior Member
        • Dec 2014
        • 705

        #4
        These work for me in VB.Net, but I used the RPC version of JSON:

        request.ContentType = "application/json-rpc"
        request.Headers.Add(HttpRequestHeader.AcceptCharse t, "ISO-8859-1,utf-8")
        request.Headers.Add("X-Authentication", ssoid)
        request.Headers.Add("X-Application", appKey)

        The following did't work for me and remain asterisked out in my code:

        ' request.headers.add("Accept: application/json")
        ' request.Headers.Add("Accept: json")

        There's another that does this, but the reasons are losts in the fogs of chronology:

        request.Headers.Add("Content-Encoding: gzip")


        This might be relevant: mine is not a WebClient, but a WebRequest.

        Comment

        • WTPooh
          Member
          • May 2012
          • 88

          #5
          Sorry. My answer was about HttpWebRequest.
          Add this line to your program:
          ServicePointManager.Expect100Continue = false;

          Code:
          using System;
          using System.Net;
          using System.Text;
          
          namespace ConsoleApp1
          {
              class Program
              {     
                  static void Main(string[] args)
                  {
                      ServicePointManager.Expect100Continue = false;
          
                      var appKey = "xxxxxx";
                      var sessionToken = "xxxxxxxxxxx";
                      var endPoint = "https://api.betfair.com/exchange/betting/rest/v1.0/listEventTypes/";
                      var request = new WebClient();  
                      request.Headers.Add("Accept", "application/json"); 
                      request.Headers.Add("Content-Type", "application/json");
                      request.Headers.Add("X-Application", appKey);
                      request.Headers.Add("X-Authentication", sessionToken);
                      var postData = "{\"filter\":{}}";
                      var bytes = Encoding.GetEncoding("UTF-8").GetBytes(postData);
                      var response = request.UploadData(new Uri(endPoint), bytes);
                  }
              }
          }

          Comment

          • PaulD
            Junior Member
            • Nov 2019
            • 4

            #6
            ServicePointManager.Expect100Continue = false;

            at start of the program fixed everything!

            Thank you very much WTPooh
            Last edited by PaulD; 16-11-2019, 12:51 AM.

            Comment

            • PaulD
              Junior Member
              • Nov 2019
              • 4

              #7
              Originally posted by jabe View Post
              These work for me in VB.Net, but I used the RPC version of JSON:

              request.ContentType = "application/json-rpc"
              request.Headers.Add(HttpRequestHeader.AcceptCharse t, "ISO-8859-1,utf-8")
              request.Headers.Add("X-Authentication", ssoid)
              request.Headers.Add("X-Application", appKey)

              The following did't work for me and remain asterisked out in my code:

              ' request.headers.add("Accept: application/json")
              ' request.Headers.Add("Accept: json")

              There's another that does this, but the reasons are losts in the fogs of chronology:

              request.Headers.Add("Content-Encoding: gzip")


              This might be relevant: mine is not a WebClient, but a WebRequest.

              ServicePointManager.Expect100Continue = false as suggested by WTPooh was the trick but thank you very much jabe for your kind reply!

              Comment

              Working...
              X