How to Receive File via REST that Requires a Header?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 797
Joined: Thu May 21, 2009 6:56 pm

How to Receive File via REST that Requires a Header?

Post by swhite »

Hi

I want to access a REST API to download a file that requires a token in the "Authorization:" key. I cannot use ReceiveHTTPFile() because there is no option to include the token in the header. I tried using OpenNetworkConnection(), SendNetworkString and then ReceiveNetworkData. All I receive is an html page with various messages but not the file. If I try it in PostMan everything works. So I wondering what I did wrong.

Below is a some sample code I used for testing. The key shown in the code is fake but the result I received shown below was generated with the real key. The actual URL is https://beta.gvmunifi.com/api/sentinel/ ... pdates/zip

Code: Select all

#AuthKey              = "MagdfgdCUfghjffjgfgjZKg8" ; Fake key
lnConn.i = OpenNetworkConnection("beta.gvmunifi.com",443,#PB_Network_TCP,5000)
If lnConn
   If SendNetworkString(lnConn,"GET /api/sentinel/KARDTECHLAB1/updates/zip"+#CRLF$+
                               "Authorization: "+#AuthKey+#CRLF$+
                               "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"+#CRLF$+
                               "Accept: */*"+#CRLF$+"Cache-Control: no-cache"+#CRLF$+
                               "Host: beta.gvmunifi.com"+#CRLF$+
                               "Accept-Encoding: gzip, deflate, br"+#CRLF$+
                               "Connection: keep-alive"+#CRLF$+#CRLF$)
      *rxBuffer=AllocateMemory(1500)
      lnLen = ReceiveNetworkData(lnConn,*rxBuffer,1500)
      ShowMemoryViewer(*rxBuffer,750)
      FreeMemory(*rxBuffer)
      
   EndIf
   CloseNetworkConnection(lnConn)
EndIf
   
; Result when using the real key      
; 0000000028F42818  3C 68 74 6D 6C 3E 0D 0A 3C 68 65 61 64 3E 3C 74  <html>..<head><t
; 0000000028F42828  69 74 6C 65 3E 34 30 30 20 54 68 65 20 70 6C 61  itle>400 The pla
; 0000000028F42838  69 6E 20 48 54 54 50 20 72 65 71 75 65 73 74 20  in HTTP request 
; 0000000028F42848  77 61 73 20 73 65 6E 74 20 74 6F 20 48 54 54 50  was sent To HTTP
; 0000000028F42858  53 20 70 6F 72 74 3C 2F 74 69 74 6C 65 3E 3C 2F  S port</title></
; 0000000028F42868  68 65 61 64 3E 0D 0A 3C 62 6F 64 79 3E 0D 0A 3C  head>..<body>..<
; 0000000028F42878  63 65 6E 74 65 72 3E 3C 68 31 3E 34 30 30 20 42  center><h1>400 B
; 0000000028F42888  61 64 20 52 65 71 75 65 73 74 3C 2F 68 31 3E 3C  ad Request</h1><
; 0000000028F42898  2F 63 65 6E 74 65 72 3E 0D 0A 3C 63 65 6E 74 65  /center>..<cente
; 0000000028F428A8  72 3E 54 68 65 20 70 6C 61 69 6E 20 48 54 54 50  r>The plain HTTP
; 0000000028F428B8  20 72 65 71 75 65 73 74 20 77 61 73 20 73 65 6E   request was sen
; 0000000028F428C8  74 20 74 6F 20 48 54 54 50 53 20 70 6F 72 74 3C  t To HTTPS port<
; 0000000028F428D8  2F 63 65 6E 74 65 72 3E 0D 0A 3C 68 72 3E 3C 63  /center>..<hr><c
; 0000000028F428E8  65 6E 74 65 72 3E 63 6C 6F 75 64 66 6C 61 72 65  enter>cloudflare
; 0000000028F428F8  3C 2F 63 65 6E 74 65 72 3E 0D 0A 3C 2F 62 6F 64  </center>..</bod
; 0000000028F42908  79 3E 0D 0A 3C 2F 68 74 6D 6C 3E 0D 0A 00 00 00  y>..</html>.....
; 0000000028F42918  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
; 0000000028F42928  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................   
Simon White
dCipher Computing
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: How to Receive File via REST that Requires a Header?

Post by JHPJHP »

Hi swhite,

EDIT: Moved FreeMemory(*Buffer)

Try the following:

Code: Select all

#AuthKey = "19044852-777D-4104-98C0-4BC48530A3F5"

URL$ = "https://beta.gvmunifi.com/api/sentinel/KARDTECHLAB1/updates/zip"
Host$ = GetURLPart(URL$, #PB_URL_Site)

NewMap Header$()
Header$("Host") = Host$
Header$("Authorization") = #AuthKey
Header$("User-Agent") = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
Header$("Accept") = "*/*"
Header$("Accept-Encoding") = "gzip, deflate, br"
Header$("Cache-Control") = "no-cache"
Header$("Connection") = "keep-alive"

HttpRequest = HTTPRequest(#PB_HTTP_Get, URL$, #Null$, #Null, Header$())

If HttpRequest
  StatusCode$ = HTTPInfo(HttpRequest, #PB_HTTP_StatusCode)

  If StatusCode$ = "200"
    *Buffer = HTTPMemory(HttpRequest)

    If *Buffer
      Size = MemorySize(*Buffer)

      If Size
        Filename$ = "update.zip"

        If CreateFile(0, Filename$)
          WriteData(0, *Buffer, Size)
          CloseFile(0)
        EndIf
        MessageRequester("HTTP", "Check File: " + Filename$, #PB_MessageRequester_Info)
      EndIf
      FreeMemory(*Buffer)
    EndIf
  Else
    MessageRequester("HTTP", "Status Code: " + StatusCode$, #PB_MessageRequester_Warning)
  EndIf
  FinishHTTP(HttpRequest)
EndIf
Last edited by JHPJHP on Sat Aug 02, 2025 3:06 pm, edited 2 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to Receive File via REST that Requires a Header?

Post by TI-994A »

Here's a working example which implements the OpenNetworkConnection() function:

> PureBasic and REST APIs


And here's one implementing the HTTPRequest() function:

> PureBasic and REST APIs - An HTTPRequest() Example


They both connect to a live, working test script on my server. I hope it helps. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to Receive File via REST that Requires a Header?

Post by infratec »

In JHPJHPs example you need only to set the Authorization header.

Code: Select all

NewMap Header$()
Header$("Authorization") = #AuthKey

HttpRequest = HTTPRequest(#PB_HTTP_Get, URL$, #Null$, #Null, Header$())
Host and ... are set from PB automatically.
You can check this with WireShark.

And ... FreeMemory(*Buffer) is at he wrong place.
Should be after the next EndIf.

And I think there is something missing in front of your key.
Like 'Bearer ksslksjfllkslksls'

Change the URL in Postman to http:// and use WireShark to see the packet.

Btw. I switched from Postman to Bruno.
Everything is stored locally.
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to Receive File via REST that Requires a Header?

Post by idle »

Also just a note, some REST servers will return Expect: 100-continue
which will cause Httprequest to finish at that point but if you add header("Expect") = ""
Then the rest server will skip that and give you the payload.
swhite
Enthusiast
Enthusiast
Posts: 797
Joined: Thu May 21, 2009 6:56 pm

Re: How to Receive File via REST that Requires a Header?

Post by swhite »

JHPJHP wrote: Fri Aug 01, 2025 10:53 pm Hi swhite,

EDIT: Moved FreeMemory(*Buffer)

Try the following:

Code: Select all

#AuthKey = "19044852-777D-4104-98C0-4BC48530A3F5"

URL$ = "https://beta.gvmunifi.com/api/sentinel/KARDTECHLAB1/updates/zip"
Host$ = GetURLPart(URL$, #PB_URL_Site)

NewMap Header$()
Header$("Host") = Host$
Header$("Authorization") = #AuthKey
Header$("User-Agent") = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
Header$("Accept") = "*/*"
Header$("Accept-Encoding") = "gzip, deflate, br"
Header$("Cache-Control") = "no-cache"
Header$("Connection") = "keep-alive"

HttpRequest = HTTPRequest(#PB_HTTP_Get, URL$, #Null$, #Null, Header$())

If HttpRequest
  StatusCode$ = HTTPInfo(HttpRequest, #PB_HTTP_StatusCode)

  If StatusCode$ = "200"
    *Buffer = HTTPMemory(HttpRequest)

    If *Buffer
      Size = MemorySize(*Buffer)

      If Size
        Filename$ = "update.zip"

        If CreateFile(0, Filename$)
          WriteData(0, *Buffer, Size)
          CloseFile(0)
        EndIf
        MessageRequester("HTTP", "Check File: " + Filename$, #PB_MessageRequester_Info)
      EndIf
      FreeMemory(*Buffer)
    EndIf
  Else
    MessageRequester("HTTP", "Status Code: " + StatusCode$, #PB_MessageRequester_Warning)
  EndIf
  FinishHTTP(HttpRequest)
EndIf
Your idea works just fine. I forgot about using HTTPMemory but it works as expected.

Thanks to everyone who responded to my post. I now have several options to solve this issue.
Simon White
dCipher Computing
Post Reply