question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting 400 Bad Request with Analytics Batch Request

See original GitHub issue

Hi. I’m having a problem with a Batch Request that I am doing. It was previously working fine a couple weeks ago, I left it for a week, came back to it and now it is giving 400 Bad Requests even when only one request is queued. It errors on await request.ExecuteAsync() with the message Response status code does not indicate success: 400 (Bad Request). For authorisation I am using a service account. Here is the code:

public async Task<List<RealtimeData>> GetRealTimeAnalyticsAsync(IEnumerable<string> tableIDs, string dimensions)
        {
            List<RealtimeData> realTimeData = new List<RealtimeData>();
            List<RequestError> requestErrors = new List<RequestError>();

            var request = new BatchRequest(_analyticsService);

            foreach (var tableId in tableIDs)
            {
                var req = _analyticsService.Data.Realtime.Get($"ga:{tableId}", "rt:activeUsers");
                req.Dimensions = dimensions;

                request.Queue<RealtimeData>(req, (content, errors, i, message) =>
                    {
                        if (content != null) realTimeData.Add(content);
                        if (errors != null) requestErrors.Add(errors);
                    });
            }

            await request.ExecuteAsync();

            return realTimeData;
        }

I know Google have been updating their Batch Request endpoints, deprecating the global endpoint. Having followed the .NET instructions on this link. I can confirm all my Google Client Libraries are up to date and from debugging the code the BatchURI and BatchURL have values https://www.googleapis.com/batch/analytics/v3.

I’m not really sure what I’m missing or what I may have done to break it. I’ve had some conversations with the Google Analytics API support team and with their help I was able to make curl requests to https://www.googleapis.com/batch/analytics/v3 fine. Also my code works fine when making single requests and not batching them.

Thanks for any help or guidance available!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
amanda-tarafacommented, Aug 25, 2020

Happy that is working for you as well!

Are you allowed to say what the issue was at all?

The problem was related to a change in service config that made Analytics batch requests requiere Content-ID to be set per each individual request. I din’t mentioned it before, sorry for that, but if you would have examined the body of the 400 response, you would have found a JSON which ultimately contained an error message saying something like “Content-ID missing for each individual request”. For the Analytics API, Content-ID is optional, the config has now been fixed to that purpose. Again, thanks for reporting!

Also I know its outside the scope of the original issue, but on a side note now batching is working, do you have any guidance on how to manage the 10 queries per second quota as described here in the docs?

(Do create a new issue next time 😃). But, I think you’d want to wait 1 second after you have received the response from the previous batch, you are now waiting 1 second in between batch starts but that means that batches might be even executing at the same time. Something like this:

       // Analytics API only allows for 10 calls per second
       if (request.Count == MaxCallsPerSecond)
       {
             await request.ExecuteAsync();
              // Add 1 sec delay between each batch request
              await Task.Delay(1000);

              // New batch request for next 10
              request = new BatchRequest(_analyticsService);
        }

I would advise you though to also ask about this on the Analytics API support channels, we are not experts on each individual API, just the client libraries, and they might have better alternatives.

I’ll close this issue now, since I believe we have addressed your problem, but don’t hesitate to create new ones if you encounter something else.

1reaction
amanda-tarafacommented, Aug 25, 2020

@WarrenH97 @erikosterholm83 The fix should now be live. My code that failed before is now working as expected. I’ll leave this issue open for a couple of days until you can confirm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving 400 Bad Request error with Google Api Batch ...
I'm having an issue with an Analytics API batch request that I am doing, it was working and now it isn't without me...
Read more >
400 Bad request when creating batch users
Hello everyone! I have an issue when creating batch contacts - I keep getting an error 400 bad request. My JSON is valid...
Read more >
(400) Bad Request from REST API
To my understanding, a 400 error means there's something wrong with the formatting of the request. Looking around, I've seen other posts that ......
Read more >
OData batch request with included services leads to error ...
I want to use a $batch request on service B to do some POST operations on service A objects and then I want...
Read more >
Conversation Aggregate query 400 bad request - Platform API
But when I posted the query throw developer-tools after setting these two metrics ,there came out a 400 bad request error.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found