Error 406 despite providing correct accept/contenttype header for VMware REST API with .NET HTTPClient?


April 2019


3 time


I'm working as an intern on a controll Application(c# WPF) for the VMware workstation REST API to:

  1. "GET" Get the list of currently available VMs on the server. (Done)
  2. "GET" Get the power-status of any VM via ID. (Done)
  3. "PUT" Get any VM to boot. (WIP)
  4. "PUT" Get any VM to shutdown. (WIP)

I'm currently using the .NET HTTPClient to send instructions to the VMServer and so far I was able to recive all the information from any possible GET command, however I couldn't figure out how to avoid getting an Error 406 from trying to use the PUT commands to power on or shutdown any VM on the server. I tried to use the following as accept header and media/content type in different combinations:

  • The VMWare specific "application/".
  • The accept all "* / *".
  • None at all (default).

The Code executing the orders is triggered by a Button Click event:

        //Needs a VMID, valid IP and valid Port, Password and User will be handled aswell, but aren't requiered to start the request
        //I also use the text in the textlabels as booleans, I will change that later if I still have time. 
        if (tb_VMID.Text.Length > 0 && lbl_InvalidIP.Content.ToString() == "valid" && lbl_InvalidPort.Content.ToString() == "valid")
            //here starts the preperation of the httpclient info
            using (HttpClientHandler _handler = new HttpClientHandler())//Handler for credentials
                _handler.Credentials = new NetworkCredential(tb_Username.Text, tb_Pass.SecurePassword);

                using (HttpClient _httpClient = new HttpClient(_handler))//Client with Credentials
                    //Header handling (Note that the passowrd is encrypded in base 64 and needs to be that way)
                    _httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + CreateBaseCode());
                    _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/"));

                    //Request preperation
                    using (HttpContent _content = new StringContent("on", Encoding.UTF8, "application/"))
                        //now here starts the PUT request, it's async but I already have 2 working async methods to GET info, would be wierd if that would be the problem, especialy since i tried the same without async.
                        var data = _httpClient.PutAsync(Link, _content);

                        tb_Response.Text = data.Result.ToString();

0 answers