Skip to main content
Answered

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

  • January 28, 2024
  • 1 reply
  • 48 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? 

Best answer by Ebenezer.Laleye

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

View original
Did this topic help you find an answer to your question?

1 reply

Forum|alt.badge.img+4

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings