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.

Batch Request returns 404 "Not Found"

See original GitHub issue

Hi guys, I’m having a recent issue

def aggregate_response(request_id, response, exception):
    if exception is not None:
        # handle exception
        print('An exception occurred: ', exception)
    else:
        # handle response
        print('request_id is: ', request_id)
        print('response is: ', response)

def batch_processor(queryset):
    service = build('customsearch', 'v1')
    batch = service.new_batch_http_request(callback=aggregate_response)

    for result_instance in queryset:
        search_query = {
            'name': result_instance.name,
            'status': result_instance.status,
        }

        query = f"{search_query['name']} {search_query['status']}"
        # query is just a name string

        batch.add(service.cse().list(
            q=query,
            cx=os.environ.get("SEARCH_ENGINE_ID"),
            hl='en',
            lr='lang_en',
            num=3,
            sort='date',
            orTerms=search_query['name'],
            key=os.environ.get("SEARCH_API_KEY"),
        ))

    batch.execute()

I keep getting an exception: An exception occurred: <HttpError 404 when requesting https://www.googleapis.com/customsearch/v1?q={q}&cx={cx}&hl=en&lr=lang_en&num=3&sort=date&orTerms={orTerms}&key={key}&alt=json returned “Not Found”> However, clicking on the link https://www.googleapis.com/customsearch/v1?q={q}&cx={cx}&hl=en&lr=lang_en&num=3&sort=date&orTerms={orTerms}&key={key}&alt=json returns valid data.

I did some digging and I think the problem is related to the batch_uri / _LEGACY_BATCH_URI = 'https://www.googleapis.com/batch' (the trailing /batch) found in the googleapiclient/http.py file.

Going to https://www.googleapis.com/batch/customsearch/v1?q={q}&cx={cx}&hl=en&lr=lang_en&num=3&sort=date&orTerms={orTerms}&key={key}&alt=json returns “Not Found”, however removing the trailing /batch from _LEGACY_BATCH_URI value doesn’t fix it.

P.S.: This was working until recently. Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
iamlordaubreycommented, Jul 14, 2018

Ok, thanks. Fingers crossed 🤞

0reactions
busunkim96commented, Feb 15, 2019

This appears to be resolved. I was able to get the expected results using the script above.

Confirmed locally using the following:

from googleapiclient.discovery import build
#from oauth2client import file, client, tools

SE_ID = "XXXX"
SEARCH_API_KEY = "XXXX"

def aggregate_response(request_id, response, exception):
    if exception is not None:
        # handle exception
        print('An exception occurred: ', exception)
    else:
        # handle response
        print('request_id is: ', request_id)
        print('response is: ', response)

terms = ['pyramid', 'pylons']

def main():
    service = build('customsearch', 'v1')
    batch = service.new_batch_http_request(callback=aggregate_response)

    for query in terms:

        batch.add(service.cse().list(
            q=query,
            cx=SE_ID,
            hl='en',
            lr='lang_en',
            num=3,
            sort='date',
            orTerms=query,
            key=SEARCH_API_KEY,
        ))

    batch.execute()

if __name__ == '__main__':
    main()
    
Read more comments on GitHub >

github_iconTop Results From Across the Web

Google API batch request returns HttpError 404 for every call ...
It seems this problem is fixed with the newest version of the python library. However, if someone still has to solve it, ...
Read more >
REST Batch endpoint returns 404? - Ask the Community
When I make a REST call to the batch.json endpoint it returns a 404 error. I don't see anything wrong with the request,...
Read more >
Known issues with Microsoft Graph
To report a known issue, see the Microsoft Graph support page. ... and listing deployment audience exclusions returns 404 Not Found .
Read more >
HTTP status and error codes for JSON | Cloud Storage
413—Payload Too Large · Attempt to upload an object larger than 5 TiB. · Attempt a Copy request that does not complete within...
Read more >
Receiving error 404 while fetching data through ... - Google Help
Receiving error 404 while fetching data through batchGetReviews API ... review per page? That's what your current request body indicates.
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