Httprequest and downloading XML FileHow to ?

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Posts: 51
Joined: Wed Mar 18, 2015 5:52 am

Httprequest and downloading XML FileHow to ?

Post by loulou2522 »

I want to download n xmlfile creating on website part
Global url.s="https://sepa-convertir.eu/spider/toto.xml"
HTTPRequest(#PB_HTTP_Get, url, "", @HttpGetEvent())
How to ?
THanks
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Httprequest and downloading XML FileHow to ?

Post by Peter »

Image
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Httprequest and downloading XML FileHow to ?

Post by Paul »

loulou2522 wrote: Mon Oct 04, 2021 2:43 pm I want to download n xmlfile creating on website part
Global url.s="https://sepa-convertir.eu/spider/toto.xml"
HTTPRequest(#PB_HTTP_Get, url, "", @HttpGetEvent())
How to ?
THanks
Just insert your domain info into the example code in the help file...

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
    Debug Result$
    Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

Global url.s="https://sepa-convertir.eu/spider/toto.xml"
HTTPRequest(#PB_HTTP_Get, url, "", @HttpGetEvent())
And remember, your Spider Basic app must also run from the same Domain or it won't work for security reasons.

See... viewtopic.php?f=6&t=2029
loulou2522
Posts: 51
Joined: Wed Mar 18, 2015 5:52 am

Re: Httprequest and downloading XML FileHow to ?

Post by loulou2522 »

Thanks Paul that'work perfectly.
But i have a problem with PDF File the file arrive corrupt localy and can't be view. I try a lot of things but noone works here is my programm
Procedure HttpGetEvent(Success, Result$, UserData)
If Success
Debug Result$

Createfile(0,"result.pdf",@toto(),#PB_Unicode)
WriteString(0,result$)
ExportFile(0, "application/pdf", #PB_localfile)
Closefile(0)
Else
Debug "HTTPRequest(): Error"
EndIf
EndProcedure

Global url.s="https://sepa-convertir.eu/spider/qrfacture.pdf"


NewMap Headers$()
Headers$("content-type") = "application/pdf;charset=Utf8"
Headers$("X-Content-Type-Options") = "nosniff"


HTTPRequest(#PB_HTTP_Get, url, "", @HttpGetEvent(),0, Headers$())
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: Httprequest and downloading XML FileHow to ?

Post by William Van Hoecke »

@Paul
And remember, your Spider Basic app must also run from the same Domain or it won't work for security reasons.
What with the domain if the code is runs as an app on an android phone ?

I created an app an run it both as

1) web-app in chrome browser on android phone
2) android app on android phone

in case 1) the 'Exportfile' tells me it created a file witch I find in the download section.
in case 2) the 'Exportfile' tells me it created a file witch I can't find anywhere (it is not created at all)

Code: Select all

...
download_file_name.s = "TEST.PDF"
mainURL.s = "https://www.some_domain.com/"
...
ReadFile(1, mainURL + download_file_name, @FileLoadedCallback())
...
...
Procedure FileLoadedCallback(Status, Filename$, File, SizeRead)
  Select status
    Case #PB_Status_Loaded
      If CreateFile(1,download_file_name,@filesavedCallback(),#PB_UTF8)
        While Eof(File) = #False
          WriteByte(1,ReadByte(File))            
        Wend
        CloseFile(File)
        ExportFile(1,"application/pdf")
        CloseFile(1)
        MessageRequester("loading (" + download_file_name + ") suceeded" + #CRLF$ + "Creating (" + download_file_name + ") on disk succeeded",#PB_MessageRequester_Ok)
      Else
        MessageRequester("loading (" + download_file_name + ") suceeded" + #CRLF$ + "Creating (" + download_file_name + ") on disk failed",#PB_MessageRequester_Ok)
      EndIf  
    Case #PB_Status_Error
        MessageRequester("loading (" + download_file_name + ") has failed",#PB_MessageRequester_Ok)
  EndSelect 
EndProcedure
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: Httprequest and downloading XML FileHow to ?

Post by William Van Hoecke »

@Paul
And remember, your Spider Basic app must also run from the same Domain or it won't work for security reasons.
What with the domain if the code is runs as an app on an android phone ?

I created an app an run it both as

1) web-app in chrome browser on android phone
2) android app on android phone

in case 1) the 'Exportfile' tells me it created a file witch I find in the download section.
in case 2) the 'Exportfile' tells me it created a file witch I can't find anywhere (it is not created at all)

Code: Select all

...
download_file_name.s = "TEST.PDF"
mainURL.s = "https://www.some_domain.com/"
...
ReadFile(1, mainURL + download_file_name, @FileLoadedCallback())
...
...
Procedure FileLoadedCallback(Status, Filename$, File, SizeRead)
  Select status
    Case #PB_Status_Loaded
      If CreateFile(1,download_file_name,@filesavedCallback(),#PB_UTF8)
        While Eof(File) = #False
          WriteByte(1,ReadByte(File))            
        Wend
        CloseFile(File)
        ExportFile(1,"application/pdf")
        CloseFile(1)
        MessageRequester("loading (" + download_file_name + ") suceeded" + #CRLF$ + "Creating (" + download_file_name + ") on disk succeeded",#PB_MessageRequester_Ok)
      Else
        MessageRequester("loading (" + download_file_name + ") suceeded" + #CRLF$ + "Creating (" + download_file_name + ") on disk failed",#PB_MessageRequester_Ok)
      EndIf  
    Case #PB_Status_Error
        MessageRequester("loading (" + download_file_name + ") has failed",#PB_MessageRequester_Ok)
  EndSelect 
EndProcedure
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Httprequest and downloading XML FileHow to ?

Post by Paul »

loulou2522 wrote: Tue Oct 05, 2021 8:23 am Thanks Paul that'work perfectly.
But i have a problem with PDF File the file arrive corrupt localy and can't be view. I try a lot of things but noone works here is my programm

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
     Debug Result$
      
    Createfile(0,"result.pdf",@toto(),#PB_Unicode)
    WriteString(0,result$)
  ExportFile(0, "application/pdf", #PB_localfile)
  Closefile(0)  
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

Global url.s="https://sepa-convertir.eu/spider/qrfacture.pdf"


NewMap Headers$()
Headers$("content-type") = "application/pdf;charset=Utf8"
Headers$("X-Content-Type-Options") = "nosniff"


HTTPRequest(#PB_HTTP_Get, url, "", @HttpGetEvent(),0, Headers$())
I don't think this works because the requested data is returned as a string in your HttpGetEvent procedure and saving a PDF as a string is not going to work. Williams code will work because it allows the saving as Bytes. Here is his code as a complete working example...

Code: Select all

Global download_file_name.s = "qrfacture.pdf"
mainURL.s = "https://sepa-convertir.eu/spider/"

Procedure filesavedCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Saved
      Debug "File correctly saved"
      
    Case #PB_Status_Error
      Debug "File saving has failed"
  EndSelect
EndProcedure

Procedure FileLoadedCallback(Status, Filename$, File, SizeRead)
  Select status
    Case #PB_Status_Loaded
      hFile=CreateFile(#PB_Any,download_file_name,@filesavedCallback(),#PB_UTF8|#PB_LocalFile)
      If hFile
        While Eof(File) = #False
          WriteByte(hFile,ReadByte(File))            
        Wend
        CloseFile(File)
        ExportFile(hFile,"application/pdf",#PB_LocalFile)
        CloseFile(hFile)
        Debug "Load/Create succeeded"
        Else
        Debug "Load/Create failed"
      EndIf  
      
    Case #PB_Status_Error
      MessageRequester("loading (" + download_file_name + ") has failed",#PB_MessageRequester_Ok)
  EndSelect 
EndProcedure

ReadFile(0, mainURL + download_file_name, @FileLoadedCallback())
Personally I would use something like this which allows the browser to do the work and gives the user the option to save the PDF or view it in the browser.

Code: Select all

url.s="https://sepa-convertir.eu/spider/qrfacture.pdf"
! downloads.open(v_url);


@William Van Hoecke

Sorry William, due to the extreme lack of features and functionality in SpiderBasic for Mobile devices, I use a different platform for developing Mobile Apps so I have no idea where it might put the files.
The APK created by SpiderBasic looks like a webpage in disguise so maybe the file gets saved in the browser folder, or maybe in the APK's installation folder, or maybe it doesn't save at all since ExportFile doesn't return anything to let you know if it was success or fail.
fofinho
Posts: 1
Joined: Fri Oct 08, 2021 4:48 pm

Re: Httprequest and downloading XML FileHow to ?

Post by fofinho »

due to the extreme lack of features and functionality in SpiderBasic for Mobile devices, I use a different platform for developing Mobile Apps
Hi, would you mind to tell us which? I have the very same problem and looking for alternatives. Thanks!
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Httprequest and downloading XML FileHow to ?

Post by Paul »

fofinho wrote: Fri Oct 08, 2021 4:52 pm Hi, would you mind to tell us which? I have the very same problem and looking for alternatives. Thanks!
B4a and B4i (there is a fee for iOS version but Android version is free)
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: Httprequest and downloading XML FileHow to ?

Post by William Van Hoecke »

Thanks Paul
Post Reply