# C# Elasticsearch 8.0.3+ 400 Bad Request media\_type\_header\_exception

**URL:** <https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816>\
**Category:** Elasticsearch\
**Tags:** docker, language-clients\
**Created:** [January 10, 2023, 9:59am UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816 "2023-01-10T09:59:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 10, 2023, 9:59am UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/1 "2023-01-10T09:59:12Z")

</div>

Just started with a new small project with Elasticsearch, while developing Elasticsearch it is currently running in Docker ([docker-elk](https://github.com/deviantony/docker-elk/)), with the added "http.cors" things:

```auto
http.cors.enabled: true
http.cors.allow-origin: "https://app.elasticvue.com"
http.cors.allow-headers: X-Requested-With,Content-Type,Content-Length,Authorization

```

Since version 8.0.3 it will send REST API compatibility headers:

[Release notes v8.0.3 | Elasticsearch .NET Client [8.0] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/release-notes-8.0.3.html)  
[REST API compatibility | Elasticsearch Guide [8.5] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-api-compatibility.html)

Somehow it doesn't correctly retrieve/send the correct version, and Elasticsearch returns a 400 Bad Request because of this.

Maybe the same is/was in the Java version? [PR on GitHub](https://github.com/elastic/elasticsearch/issues/92658), but I'm not sure.

This is the response I get is:

```json
{
	"error": {
		"root_cause": [
			{
				"type": "media_type_header_exception",
				"reason": "Invalid media-type value on headers [Accept, Content-Type]"
			}
		],
		"type": "media_type_header_exception",
		"reason": "Invalid media-type value on headers [Accept, Content-Type]",
		"caused_by": {
			"type": "status_exception",
			"reason": "Accept version must be either version 8 or 7, but found 0. Accept=application/vnd.elasticsearch+json;compatible-with=0"
		}
	},
	"status": 400
}

```

So it doesn't see a version.

When running it in a testcase, it works. But when deployed it doesn't.

The .NET version is, sadly, .NET Framework 4.8, which is a limitation, cannot upgrade it to .NET (Core) because it's a SAAS.

When I revert back to 8.0.2, it works fine.

Should I make a bug on GitHub?

Kind regards!

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [January 11, 2023, 3:14pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/2 "2023-01-11T15:14:26Z")

</div>

Hi, @KoalaBear.

If this works locally but not when deployed, possibly something in the way it is deployed is breaking the logic which loads the version from our assembly info. We[saw this for a user](https://discuss.elastic.co/t/nest-product-check-call-fails-every-time/306348/2) who was creating a single DLL causing the version lookup to fail. Are you packaging your application in an unusual way that might cause a similar behaviour?

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 11, 2023, 3:45pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/3 "2023-01-11T15:45:03Z")

</div>

Hi Steve,

That's some good thoughts 👍 Yes and no. 😅

We do use ILmerge for some of our own DLL, but the Elasticsearch one's are not merged at all.

Couldn't paste an image, so:

```auto
 Length Name
 ------ ----
4018176 Elastic.Clients.Elasticsearch.dll
 253440 Elastic.Transport.dll
  22144 Microsoft.Bcl.AsyncInterfaces.dll
 327168 OurDlls.Merged.dll
 273920 StructureMap.dll
  98184 System.Diagnostics.DiagnosticSource.dll
  18024 System.Runtime.CompilerServices.Unsafe.dll
  76904 System.Text.Encodings.Web.dll
 513128 System.Text.Json.dll

```

So it should be able to get the versions from these DLLs. I will (ASAP) try to run those version 'retrieval' lines deployed and see what it returns and come back to you. Thanks!

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 11, 2023, 4:59pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/4 "2023-01-11T16:59:34Z")

</div>

OK, I don't use NEST of course, but I've tried it with `ElasticsearchClient` and it didn't find the assembly location. 🥳

```csharp
string location = typeof(ElasticsearchClient).GetTypeInfo().Assembly.Location;
Context.Log(LogLevel.Error, $"ElasticsearchClient assembly location: {location}");

string version = FileVersionInfo.GetVersionInfo(location)?.ProductVersion;
Context.Log(LogLevel.Error, $"ElasticsearchClient assembly version: {version}");

```

It's running on a SAAS and I know it's running inside Azure Service Fabric, that's all I know, so it might be security permissions of some sort.

Is there anything else I can try to give more debug info?

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 12, 2023, 7:51am UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/5 "2023-01-12T07:51:26Z")

</div>

Tried some more things, only the GetEntryAssembly locations is returning anything, but that is NOT 'our' DLL. So cannot even get our own version in this way.

```auto
GetExecutingAssembly location: 
Error GetExecutingAssembly version: The path is not of a legal form.

GetEntryAssembly location: D:\SvcFab\***\ConnectExecutorActor.exe
GetEntryAssembly version: 1.0.0

GetCallingAssembly location: 
Error GetCallingAssembly version: The path is not of a legal form.

ElasticsearchClient assembly location: 
Error getting assembly version: The path is not of a legal form.

```

Code:

```csharp
try
{
	Assembly executingAssembly = Assembly.GetExecutingAssembly();
	string executingAssemblyLocation = executingAssembly.Location;
	Context.Log(LogLevel.Error, $"GetExecutingAssembly location: {executingAssemblyLocation}");
	string executingAssemblyVersion = FileVersionInfo.GetVersionInfo(executingAssemblyLocation)?.ProductVersion;
	Context.Log(LogLevel.Error, $"GetExecutingAssembly version: {executingAssemblyVersion}");
}
catch (Exception ex)
{
	Context.Log(LogLevel.Error, $"Error GetExecutingAssembly version: {ex.Message}");
}

try
{
	Assembly entryAssembly = Assembly.GetEntryAssembly();
	string entryAssemblyLocation = entryAssembly.Location;
	Context.Log(LogLevel.Error, $"GetEntryAssembly location: {entryAssemblyLocation}");
	string entryAssemblyVersion = FileVersionInfo.GetVersionInfo(entryAssemblyLocation)?.ProductVersion;
	Context.Log(LogLevel.Error, $"GetEntryAssembly version: {entryAssemblyVersion}");
}
catch (Exception ex)
{
	Context.Log(LogLevel.Error, $"Error GetEntryAssembly version: {ex.Message}");
}

try
{
	Assembly callingAssembly = Assembly.GetCallingAssembly();
	string callingAssemblyLocation = callingAssembly.Location;
	Context.Log(LogLevel.Error, $"GetCallingAssembly location: {callingAssemblyLocation}");
	string callingAssemblyVersion = FileVersionInfo.GetVersionInfo(callingAssemblyLocation)?.ProductVersion;
	Context.Log(LogLevel.Error, $"GetCallingAssembly version: {callingAssemblyVersion}");
}
catch (Exception ex)
{
	Context.Log(LogLevel.Error, $"Error GetCallingAssembly version: {ex.Message}");
}

try
{
	Assembly elasticAssembly = typeof(ElasticsearchClient).GetTypeInfo().Assembly;

	if (elasticAssembly is null)
	{
		Context.Log(LogLevel.Error, $"ElasticsearchClient assembly: NULL");
	}

	string elasticLocation = elasticAssembly.Location;
	Context.Log(LogLevel.Error, $"ElasticsearchClient assembly location: {elasticLocation}");

	string elasticVersion = FileVersionInfo.GetVersionInfo(elasticLocation)?.ProductVersion;
	Context.Log(LogLevel.Error, $"ElasticsearchClient assembly version: {elasticVersion}");
}
catch (Exception ex)
{
	Context.Log(LogLevel.Error, $"Error GetCallingAssembly version: {ex.Message}");
}

try
{
	Assembly elastic2Assembly = typeof(ElasticsearchClient).Assembly;

	if (elastic2Assembly is null)
	{
		Context.Log(LogLevel.Error, $"elastic2searchClient assembly: NULL");
	}

	string elastic2Location = elastic2Assembly.Location;
	Context.Log(LogLevel.Error, $"elastic2searchClient assembly location: {elastic2Location}");

	string elastic2Version = FileVersionInfo.GetVersionInfo(elastic2Location)?.ProductVersion;
	Context.Log(LogLevel.Error, $"elastic2searchClient assembly version: {elastic2Version}");
}
catch (Exception ex)
{
	Context.Log(LogLevel.Error, $"Error GetCallingAssembly version: {ex.Message}");
}

```

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 12, 2023, 8:59am UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/6 "2023-01-12T08:59:10Z")

</div>

Sorry for the spamming 😅

The only thing that works is:

```csharp
Assembly elasticAssembly = typeof(ElasticsearchClient).Assembly;
string version = ((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(elasticAssembly, typeof(AssemblyFileVersionAttribute), false)).Version;
Version elasticVersion = new(version);
Context.Log(LogLevel.Error, $"GetCustomAttribute AssemblyFileVersionAttribute version: {version}, Major: {elasticVersion.Major}");

```

Result:

```auto
GetCustomAttribute AssemblyFileVersionAttribute version: 8.0.2.0, Major: 8

```

Also available as a oneliner:

```csharp
Version elasticVersion = new(typeof(ElasticsearchClient).Assembly?.GetCustomAttribute<AssemblyFileVersionAttribute>().Version);

```

It's also better than using `FileVersionInfo.GetVersionInfo()` I think.

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 25, 2023, 5:00pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/7 "2023-01-25T17:00:10Z")

</div>

Could you check my replies? @stevejgordon

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [January 25, 2023, 5:30pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/8 "2023-01-25T17:30:14Z")

</div>

Sorry, @KoalaBear.

Discuss doesn't notify on replies without the '@' mention, so I missed yours.

I've raised [an issue](https://github.com/elastic/elasticsearch-net/issues/7180) because we should address this as a bug in the client. Thanks for digging into this in your scenario.

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 25, 2023, 5:48pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/9 "2023-01-25T17:48:36Z")

</div>

NP, I thought so 😇 and did a gentle bump, great 👍

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [January 27, 2023, 12:01pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/10 "2023-01-27T12:01:07Z")

</div>

@KoalaBear Can you please try the latest 8.0.5 release? I updated transport to be more robust in version detection, so hopefully, that resolves your issue with the invalid REST API compatibility header.

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 27, 2023, 12:02pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/11 "2023-01-27T12:02:08Z")

</div>

Hey @stevejgordon

Thanks. I already saw and put a 👍 on the GitHub release. Will try to test it this afternoon 🙂 and let you know.

---

<div class="post-metadata">

**Author:** ![KoalaBear](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/koalabear/32/115755_2.png) [@KoalaBear](https://discuss.elastic.co/u/KoalaBear)\
**Post date:** [January 27, 2023, 12:08pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/12 "2023-01-27T12:08:39Z")

</div>

Haha, it went a little quicker as expected. It is now working with the newest 8.0.5 version 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 24, 2023, 12:09pm UTC](https://discuss.elastic.co/t/c-elasticsearch-8-0-3-400-bad-request-media-type-header-exception/322816/13 "2023-02-24T12:09:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
