400 Bad Request when trying to query elasticsearch using HttpClient

Hi @stevejgordon ,

Thank you for providing me with your help. That would make a lot of sense why PostAsJsonAsync() was not allowing me to perform the request. Sorry, I am a bit confused. If the variable response is a JSON string, shouldn't we need to serialize it into a JSON object not deserialise it since it is already a string? Please correct me if I am wrong. Yes, I have used NEST prior to this but just wanted to just use httpclient. I know it not the most ideal way in doing this.

On another note, is there a way to use string interpolation to replace certain parts of the query variable without having to create a class?

I am trying to replace some parts of my query with a variable that holds a certain value.

For instance, inside of my variable query I am wanting to replace

  • \"level\": \"Information\"
  • and \"match\": { \"level\": \"Error\"
  • with variable fieldName and variable fieldValue respectively
    • The fieldName holds a value of "_source.level"
    • and fieldValue holds a value of "information" and "error".

Snippet of the json file that I am reading from and assigning fieldname and fieldvalue to.

"LogFieldsMapping": {
	"IDField": { "FieldName": "_id" },
	"LevelField": {
		"FieldName": "_source.level",
		"FieldValue": [
			{ "value": "Information" },
			{ "value": "Error" }
		]
	}
}

The issue I am running into is normally I know you would add a $ before the string and add a {} where you want to call the variable. However, when I add a $ at the beginning of my string it does not like it and says

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

So I am facing issues trying to figure out how I can pass those variables into my query variable. I will edit the original post. I am reading from a JSON file and assigning a variable to those values that I want to grab and insert inside of the query variable. So I am just wanting to use string interpolation in my case and not create a class.