Page 1 of 1

PB5.72 - New HTTP feature to read headers not working

Posted: Tue May 19, 2020 10:29 pm
by nalor
Hi!
Please help me - I can't figure out how to use the new feature to get the http headers.

Here's my example:

Code: Select all

EnableExplicit

Define sUrl.s="https://www.purebasic.com/download/PureBasic_Demo.zip"
Define iConn.i
Define iProgress.i

InitNetwork()

iConn=ReceiveHTTPMemory(sUrl, #PB_HTTP_Asynchronous)

If Not iConn
	Debug "Error"
	End
EndIf

Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"

Repeat
	iProgress = HTTPProgress(iConn)
	Select iProgress
		Case #PB_HTTP_Success
			Debug "iConn finished (size: )"
			End
		
		Case #PB_HTTP_Failed
			Debug "iConn failed"
			FinishHTTP(iConn)
			End
		
		Case #PB_HTTP_Aborted
			Debug "iConn aborted"
			FinishHTTP(iConn)
			End
		
		Default
		
			Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"
			Debug "Current iConn: " + iProgress
	
	EndSelect
	
	Delay(500) ; Don't stole the whole CPU
ForEver
But the command >HTTPInfo(iConn, #PB_HTTP_Headers)< never returns anything useful.

Where's my error?

Thanks for help :)

Re: PB5.72 - New HTTP feature to read headers not working

Posted: Tue May 19, 2020 10:38 pm
by helpy
Read PureBasic help and check the syntax of ReceiveHTTPMemory() ...

Re: PB5.72 - New HTTP feature to read headers not working

Posted: Tue May 19, 2020 11:14 pm
by nalor
But I don't want the data in my example - all I want is the headers ... to read the contentsize from it and calculate the progress during the async download.
(I know in my example the data is just buffered and finally lost because it's never read from the buffer - but I only care about 'HTTInfo' that isn't working as expected.)

Before 5.72 it wasn't possible with a single request to get the headers and download, but now it should be - just that I can't figure out how to use the new feature correctly.

Re: PB5.72 - New HTTP feature to read headers not working

Posted: Wed May 20, 2020 7:08 am
by infratec
As you can read in the help of HTTPInfo(),
it is only valid for HTTPRequest() and HTTPRequestMemory()
and not for ReceiveHTTPMemory()

So you need

Code: Select all

EnableExplicit

Define sUrl.s="https://www.purebasic.com/download/PureBasic_Demo.zip"
Define iConn.i
Define iProgress.i

InitNetwork()

iconn = HTTPRequest(#PB_HTTP_Get, sUrl, "", #PB_HTTP_HeadersOnly)
If iconn
  Debug "Headers >"+HTTPInfo(iConn, #PB_HTTP_Headers)+"<"
  FinishHTTP(iconn)
EndIf

iConn=ReceiveHTTPMemory(sUrl, #PB_HTTP_Asynchronous)

If Not iConn
   Debug "Error"
   End
EndIf

Repeat
   iProgress = HTTPProgress(iConn)
   Select iProgress
      Case #PB_HTTP_Success
        Debug "iConn finished (size: )"
         End
      
      Case #PB_HTTP_Failed
         Debug "iConn failed"
         FinishHTTP(iConn)
         End
      
      Case #PB_HTTP_Aborted
         Debug "iConn aborted"
         FinishHTTP(iConn)
         End
      
      Default
         Debug "Current iConn: " + iProgress
   
   EndSelect
   
   Delay(500) ; Don't stole the whole CPU
ForEver
#PB_HTTP_HeadersOnly does only fetch the header and not the content.

Re: PB5.72 - New HTTP feature to read headers not working

Posted: Wed May 20, 2020 11:43 am
by helpy
helpy wrote:Read PureBasic help and check the syntax of ReceiveHTTPMemory() ...
:oops: :oops: ... oops... sorry... I have confused the two commands ReceiveHTTPMemory and HTTPRequestMemory.

Re: PB5.72 - New HTTP feature to read headers not working

Posted: Wed May 20, 2020 9:30 pm
by nalor
Thanks for the help! Didn't notice this small detail ...

Problem solved :)