How to Use Cube.js API with .NET Core Application? Facing "Query Param is Required" Error

NALB 71 Reputation points
2025-01-12T00:32:58.51+00:00

Hello,

I am trying to integrate the Cube.js API into my .NET Core application. My goal is to fetch data from Cube.js using a custom query. However, I keep encountering an error and can't seem to get the integration working.

Here’s the method I implemented based on an example I found:

public async Task<string> GetRoomOccupancyDataAsync()
{
    var query = new
    {
        measures = new[] { "room_occupancies.count" },
       dimensions = new[]
       {
           "room_occupancies.roomtype",
           "room_occupancies.checkindate"
        },
        limit = 10
    };
    var jsonQuery = JsonSerializer.Serialize(query);
    var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:4000/cubejs-api/v1/load")
    {
        Content = new StringContent(jsonQuery, Encoding.UTF8, "application/json")
    };
    var response = await _httpClient.SendAsync(request);
    if (!response.IsSuccessStatusCode)
    {
        var errorContent = await response.Content.ReadAsStringAsync();
        throw new HttpRequestException($"Error: {response.StatusCode}, Details: {errorContent}");
    }
    return await response.Content.ReadAsStringAsync();
}

and the response

{`` ``"error":`` ``"Query param is required",`` ``"stack":`` ``"Error: Query param is required\n at ApiGateway.parseQueryParam (/cube/node_modules/@cubejs-backend/api-gateway/src/gateway.ts:2000:13)\n at ApiGateway.load (/cube/node_modules/@cubejs-backend/api-gateway/src/gateway.ts:1699:20)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at /cube/node_modules/@cubejs-backend/api-gateway/src/gateway.ts:305:7",`` ``"requestId":`` ``"5573e971-9aa4-4d8c-a302-5fe6366f8885-span-1"`` ``}
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2025-01-13T05:25:52.1633333+00:00

    Accroding to the document,the data format should be like:'{"query": {"measures":["users.count"]}}'

    modify

    var query = new 
    {
      measures = new[] { "room_occupancies.count" },
      dimensions = new[] { "room_occupancies.roomtype", "room_occupancies.checkindate" }, 
      limit = 10 
    };
    
    var jsonQuery = JsonSerializer.Serialize(query);
    

    to

    var query = new
    {
        query = new
        {
            measures = new[] { "room_occupancies.count" },
            dimensions = new[]
           {
               "room_occupancies.roomtype",
               "room_occupancies.checkindate"
            },
            limit = 10
        }
        
    };
    var jsonQuery = JsonSerializer.Serialize(query);
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Ruikai Feng


  2. Jack Dang (WICLOUD CORPORATION) 1,020 Reputation points Microsoft External Staff
    2025-07-24T09:15:47.4666667+00:00

    Hello @NALB,

    Thank you for sharing the details of the issue you're encountering with the Cube.js API integration in your .NET Core application. The error message "Query param is required" indicates that the Cube.js API expects the query payload to be structured with a query key wrapping the actual query parameters, as per the Cube.js documentation.

    To resolve this, you need to adjust the structure of the query object in your code to include the query key. Below is the corrected version of your code:

    var query = new
    {
        query = new
        {
            measures = new[] { "room_occupancies.count" },
            dimensions = new[]
            {
                "room_occupancies.roomtype",
                "room_occupancies.checkindate"
            },
            limit = 10
        }
    };
     
    var jsonQuery = JsonSerializer.Serialize(query);
    

    The Cube.js API expects the JSON payload to have a top-level query object, like this: {"query": {"measures": ["room_occupancies.count"], dimensions: [...], "limit": 10}}. By wrapping your query parameters in a query object, as shown above, the API should process the request correctly.

    Hope my answer helps!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.