0

I am getting this error:

Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid

On a brand new site that is using https.

My bindings look like this:

enter image description here

I tried test.example.com and * as my hostname as well.

I am using a certificate that I got from GoDaddy and validated using one of those online validation services (after installation).

I have been chasing this error for several hours and about to go crazy. Other people are reporting this error but they are reporting it on http or IIS7. I am on IIS10 so the IIS7 solutions don't apply.

I am on Amazon EC2.

I suspect the following problem but I don't know how to fix it:

Somehow I have to tell the machine (EC2AMAZ-XYZ) that has the same name as my certificate (test.example.com).

This command will run fine:

curl https://test.example.com:8028

But this command will fail with the above error:

curl -d '{"UserName":"JOHN", "Password":"CHANGEME"}' -H "Content-Type: application/json" -X POST https://test.example.com:8028/api/Account/login

If I use https://test.example.com:8028 in a browser it works fine, but the I can't run the POST command in a browser.

We added a GET based version of Login and it fails with same error. We also enabled http and it also fails.

Here is the code related to this problem:

using System.Web.Http;
using Newtonsoft.Json.Serialization;

namespace TappDmz
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            // Route to index.html
            config.Routes.MapHttpRoute(
                name: "Index",
                routeTemplate: "{id}.html",
                defaults: new { id = "index" });

            config.Routes.MapHttpRoute(
             name: "DefaultApi",
             routeTemplate: "api/{controller}/{id}",
             defaults: new { id = RouteParameter.Optional });
            //Now set the serializer setting for JsonFormatter to Indented to get Json Formatted data  
            config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            //For converting data in Camel Case  
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
    }
}
Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
  • also post this or share this SO link to https://aws.amazon.com/developer/community/ – Softhmak. Dec 09 '18 at 05:41
  • What error comes if you use a web browser like IE/Chrome? – Lex Li Dec 09 '18 at 05:45
  • Lex Li: This "curl https://test.example.com:8028" command says that it works fine on web browser. – Softhmak. Dec 09 '18 at 05:53
  • @Softhmak. I am more interesting in something like Swagger https://en.wikipedia.org/wiki/Swagger_(software) – Lex Li Dec 09 '18 at 19:22
  • @LexLi: the line "If I use https://test.example.com:8028 in a browser it works fine, but the I can't run the POST command in a browser." clearly answers your question/confusion. – Softhmak. Dec 09 '18 at 23:03
  • 1
    @Michael Potter Could you add `-v` to your curl command and paste the output in your question? – Ray Baxter Dec 11 '18 at 05:36
  • @RayBaxter Softhmak got it working so I can't reproduce the error. he changed quite a bit of stuff so I am not 100% sure he will know what fixed. I am upvoting your comment because it is a good suggestion tho. – Be Kind To New Users Dec 11 '18 at 05:50

1 Answers1

1

Command line curl is having difficulty figuring out what host you are trying to connect to. Try this

curl -H 'Host: test.example.com' -d '{"UserName":"JOHN", "Password":"CHANGEME"}' -H "Content-Type: application/json" -X POST https://test.example.com:8028/api/Account/login

See here

Ray Baxter
  • 3,181
  • 23
  • 27
  • Along these lines tho: Do I need to tell EC2 the name of the machine so some kind of reverse lookup would work? right now it has the encoded AWS name. I point to it with DNS with my name, but Amazon does not know that. – Be Kind To New Users Dec 10 '18 at 00:15