Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
daveParso
Frequent Visitor

PostRowsInGroups is giving badRequest from Json object

I have the following code to create a json object and then post this to a streaming dataset in powerBI. With the code i have so far i seem to be finding the dataset and table just fine but i am getting a badrequest on my json object. Is there something simple im missing in the code below that is preventing this from working?

 

using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
    var payload = new testValues
    {
        value1 = "test",
        value2 = counter,
        value3 = DateTime.UtcNow
    };
    string stringPayload = JsonConvert.SerializeObject(payload);
    StringContent postContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
            
    ODataResponseListDataset datasets = client.Datasets.GetDatasetsInGroup(GroupId);
    client.Datasets.PostRowsInGroup(GroupId, datasets.Value.LastOrDefault().Id, "RealTimeData", postContent);
    counter++;
    return;
}

and below is the testValues class just incase that can also shed some light on my issue here:

public class testValues
{
    [JsonProperty("value1")]
    public string value1 { get; set; }

    [JsonProperty("value2")]
    public int value2 { get; set; }

    [JsonProperty("value3")]
    public DateTime value3 { get; set; }
}
1 ACCEPTED SOLUTION
Eric_Zhang
Employee
Employee

@daveParso

You can add try..catch blocks to get the bad request details.

try
                {
                    client.Datasets.PostRowsInGroup(groupId, yourdatesetid, "RealTimeData", dataObj);
                }
catch (HttpOperationException ex)
               { 
                    //Bad Request
                    var content = ex.Response.Content;
                    Console.WriteLine(content); 
                }

Based on my test, my below demo work.

 

        static void Main(string[] args)
        {
            var credentials = new TokenCredentials(accessToken);
            using (var client = new PowerBIClient( credentials))
            {
                var payload = new testValues
                {
                    value1 = "test",
                    value2 = 1,
                    value3 = DateTime.UtcNow
                   // value3 = DateTime.UtcNow.ToString("s") + "Z"
                };

                IList<testValues> list = new List<testValues>();

                list.Add(payload);

                var stringPayload = JsonConvert.SerializeObject(list);
                object dataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(stringPayload.ToString());
                //StringContent postContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                try
                {
                    client.Datasets.PostRowsInGroup(groupId,datasetId, "RealTimeData", dataObj);
                }
                catch (HttpOperationException ex)
               { 
                    //Bad Request
                    var content = ex.Response.Content;
                    Console.WriteLine(content); 
                }
                 
            }

            Console.WriteLine("Done");
            Console.ReadKey();

        }

View solution in original post

1 REPLY 1
Eric_Zhang
Employee
Employee

@daveParso

You can add try..catch blocks to get the bad request details.

try
                {
                    client.Datasets.PostRowsInGroup(groupId, yourdatesetid, "RealTimeData", dataObj);
                }
catch (HttpOperationException ex)
               { 
                    //Bad Request
                    var content = ex.Response.Content;
                    Console.WriteLine(content); 
                }

Based on my test, my below demo work.

 

        static void Main(string[] args)
        {
            var credentials = new TokenCredentials(accessToken);
            using (var client = new PowerBIClient( credentials))
            {
                var payload = new testValues
                {
                    value1 = "test",
                    value2 = 1,
                    value3 = DateTime.UtcNow
                   // value3 = DateTime.UtcNow.ToString("s") + "Z"
                };

                IList<testValues> list = new List<testValues>();

                list.Add(payload);

                var stringPayload = JsonConvert.SerializeObject(list);
                object dataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(stringPayload.ToString());
                //StringContent postContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                try
                {
                    client.Datasets.PostRowsInGroup(groupId,datasetId, "RealTimeData", dataObj);
                }
                catch (HttpOperationException ex)
               { 
                    //Bad Request
                    var content = ex.Response.Content;
                    Console.WriteLine(content); 
                }
                 
            }

            Console.WriteLine("Done");
            Console.ReadKey();

        }

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.