Download only part of a file with ReceiveHTTPMemory()?

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Download only part of a file with ReceiveHTTPMemory()?

Post by BarryG »

There's a large text file (1 MB) that I'm currently downloading with ReceiveHTTPMemory() but I really only need the first 1 KB of it. Downloading the entire 10 MB just to use Left() on the data is silly. The ReceiveHTTPMemory() command doesn't have a flag to stop downloading after X bytes, so how would I do that? Thanks.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 563
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Download only part of a file with ReceiveHTTPMemory()?

Post by Sicro »

ReceiveHTTPMemory() does not support header parameters, so use HTTPRequest() instead:

Code: Select all

NewMap header$()
header$("Range") = "bytes=0-1023"

request = HTTPRequest(#PB_HTTP_Get, "https://...", "", 0, header$())
If request
  response$ = HTTPInfo(request, #PB_HTTP_Response)
  Debug StringByteLength(response$, #PB_Ascii)
  FinishHTTP(request)
Else
  Debug "Error"
EndIf
However, you must ensure that the server supports HTTP requests with ranges.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply