Python Forum
request.get to read large response
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
request.get to read large response
#1
I am trying to use curl to read read large response.
I ran that from command line and redirected to text file and it is 30Mb file

using python I can put one record at a time but not everything that I want.
using following code which is simple.

    try:
        r = requests.get(url, auth=('username', 'password'), headers=headers, verify=False)
        r.raise_for_status()
    except requests.exceptions.RequestException as error:  
        print("Error:", error)
         exit()

    output = r.json()
Reply
#2
But what is the output of your code? Does it show the error message or the json?
Reply
#3
(Apr-05-2022, 08:11 AM)ibreeden Wrote: But what is the output of your code? Does it show the error message or the json?

if I pull one record it shows in json format.

if I do try to pull all the record it fails with error.
I can do same via curl command on Linux and redirect that to text file and I have 30mb file with all json data in it.
Reply
#4
Did you look at Streaming requests? Would it work in your case?
Or a package like json_stream?
pythonlearner1 and ndc85430 like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Apr-05-2022, 02:24 PM)buran Wrote: Did you look at Streaming requests? Would it work in your case?
Or a package like json_stream?

Great, json_stream worked. now how do I encode it

with requests.get(url, auth=('username', 'password'),headers=headers, verify=False, stream=True) as response:
            data = json_stream.requests.load(response)
         print(type(data))
<class 'json_stream.base.TransientStreamingJSONObject'>
Reply
#6
The point of streaming is that the server sends chunks of data at a time, as it's ready. So, you'll need to have read the whole thing before processing the data. The docs should tell you how to do that with this library.
Reply
#7
I try two method describe in that document but didn't work that is why ask. searching around didn't find any example either.

here is what I try

# Option 1: supply json_stream.encoding.default as the default argument
print(json.dumps(data, default=default))

# Option 2: supply json_stream.encoding.JSONStreamEncoder as the cls argument
# This allows you to created your own subclass to further customise encoding
print(json.dumps(data, cls=JSONStreamEncoder))

both prints empty dictionary.
Reply
#8
It is working with just request.get without json_stream. it was timing issue.

I have added timeout=200 and I got the whole result back.

Thank you everyone who help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 5,105 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Unable to convert request response. johnboy1974 4 3,065 Apr-05-2021, 10:10 AM
Last Post: snippsat
  can't read QRcode in large file simoneek 0 1,520 Sep-16-2020, 08:52 AM
Last Post: simoneek
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 2,071 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Empty response to request causing .json() to error t4keheart 1 10,111 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,954 Jun-18-2020, 08:07 AM
Last Post: buran
  Issues parsing the response from a request garnold 3 2,578 May-14-2019, 12:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020