Answered

Error "Query body must contain a query hash" when searching contacts

  • 28 January 2024
  • 1 reply
  • 25 views

Hello,

I trying to search by using the “POST /contacts/search” API. If I call the API from either POSTMAN or form the JSON as string, everything works fine. But when I trying to serialize an object to JSON and call the API, I get the error “Query body must contain a query hash”.

This is from .NET API.

Scenario which works

var json = JObject.Parse($@"{{
    ""query"": {{
        ""operator"": ""AND"",
        ""value"": [
            {{
                ""field"": ""email"",
                ""operator"": ""="",
                ""value"": ""{email}""
            }}
        ]
    }}
}}");

var jsonStr = json.ToString();

var postData = new StringContent(jsonStr, Encoding.UTF8, "application/json");

using (HttpResponseMessage response = await httpClient.PostAsync($"/contacts/search", postData))
{ … }

Scenario which results in the error

var searchQuery = new IntercomSearchQuery
{
    Operator = "AND",
    Value = new List<IntercomSearchValue>
    {
        new IntercomSearchValue
        {
            Field = "email",
            Operator = "=",
            Value = email
        }
    }
};

var searchQueryStr = JsonConvert.SerializeObject(searchQuery);

var postData = new StringContent(searchQueryStr, Encoding.UTF8, "application/json");

using (HttpResponseMessage response = await httpClient.PostAsync($"/contacts/search", postData))

{ … }

// Classes
using Newtonsoft.Json;

public class IntercomSearchQuery
{
[JsonProperty("operator")]
public required string Operator { get; set; }

[JsonProperty("value")]
public required List<IntercomSearchValue> Value { get; set; }
}

public class IntercomSearchValue
{
[JsonProperty("field")]
public required string Field { get; set; }

[JsonProperty("operator")]
public required string Operator { get; set; }

[JsonProperty("value")]
public required string Value { get; set; }
}

Question

Would love some guidance on what I might be doing wrong here? 

icon

Best answer by Ebenezer.Laleye 31 January 2024, 16:09

View original

1 reply

Userlevel 2
Badge +3

Hi @mitren-usapath ! Ebenezer here from Engineering Support👋.

Could you add a 'Content-Type' header with the value of 'application/json' to see if that returns a successful response?

Let me know if this works for you

Reply