Page 1 of 1

LibCurl delete a remote file [Greatly resolved]

Posted: Sat Aug 10, 2024 4:51 pm
by Kwai chang caine
Hello at all :D

I continue my hard expedition in CurLand, without succes i have test with "DELE", "RMD", RMDA", "DELETE" :|
This time, i want delete a file :oops:
Curl wrote:Error: Quote command returned error

Code: Select all

#REMOTE_FILE = "libcurl.pbi"
#URL$ = "sftp://MyFtpServer.fr/var/www/html/" + #REMOTE_FILE
#Username$ = "XXXX"
#Password$ = "XXXXX" 

#LibCurl_ExternalDLL = #True
IncludeFile "libcurl.pbi"

Define curl.i, res.i, *headerlist

InitNetwork()

curl_global_init(#CURL_GLOBAL_DEFAULT)
curl = curl_easy_init()

If curl

 *headerlist = curl_slist_append(*headerlist, "DELETE " + #REMOTE_FILE)
 curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @LibCurl_WriteFunction())
 curl_easy_setopt_str(curl, #CURLOPT_URL, #URL$)
 curl_easy_setopt_str(curl, #CURLOPT_USERNAME, #Username$)
 curl_easy_setopt_str(curl, #CURLOPT_PASSWORD, #Password$) 
 curl_easy_setopt(curl, #CURLOPT_POSTQUOTE, *headerlist)
 curl_easy_setopt(curl, #CURLOPT_USE_SSL, #CURLUSESSL_ALL)
 
 ; accept insecure certificates
 curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, #False)
 curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, #False)
 
 res = curl_easy_perform(curl)
 
 If res = #CURLE_OK
  Debug "Ok"
 Else
  Debug "Error: " + curl_easy_strerror(res)
 EndIf
 
 curl_slist_free_all(*headerlist)
 curl_easy_cleanup(curl)
 curl_global_cleanup()
 
EndIf
Have a good day

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 4:58 pm
by Marc56us
Try add -X

Code: Select all

curl -X DELETE [URL]
(not tested)
:wink:

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:02 pm
by Kwai chang caine
Hello Marc56 :wink:

The problem is i not use the executable, but the wrapper of INFRATEC and that not works if i replace the "DELETE " by "-x DELETE " :|
Surely because the -x is a parameter of the exe :wink:

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:12 pm
by Marc56us
-X
(I don't if case sensitive ?)
?
curl_easy_setopt_str(*HTTPRequest\curl, #CURLOPT_CUSTOMREQUEST, "-X DELETE")
Not tested

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:22 pm
by Kwai chang caine
Unfortunately that not works :|
On the other hand, i have a "OK" return :D

But the file is still there... straight as an "I", and when I look at it with a magnifying glass

Image

i believe he have no real respect for my programming "talent" :| :mrgreen:

Code: Select all

If curl
 curl_easy_setopt_str(curl, #CURLOPT_CUSTOMREQUEST, "-X DELETE")
 curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @LibCurl_WriteFunction())
 curl_easy_setopt_str(curl, #CURLOPT_URL, #URL$)
 curl_easy_setopt_str(curl, #CURLOPT_USERNAME, #Username$)
 curl_easy_setopt_str(curl, #CURLOPT_PASSWORD, #Password$) 
 curl_easy_setopt(curl, #CURLOPT_USE_SSL, #CURLUSESSL_ALL)
 
 ; accept insecure certificates
 curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, #False)
 curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, #False)
 
 res = curl_easy_perform(curl)
 
 If res = #CURLE_OK
  Debug "Ok"
 Else
  Debug "Error: " + curl_easy_strerror(res)
 EndIf
 
 curl_slist_free_all(*headerlist)
 curl_easy_cleanup(curl)
 curl_global_cleanup()
 
EndIf 
Edit

i have also try

Code: Select all

curl_easy_setopt_str(curl, #CURLOPT_CUSTOMREQUEST, "-X DELETE " + #REMOTE_FILE)
and it's not better :|

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:28 pm
by Marc56us
Making progress :D
Does user have rights to delete file in this dir ?

Test with curl in command line (Windows 10 now has curl natively)

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:41 pm
by Fred
Try 'rm yourfilename'

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 5:58 pm
by Kwai chang caine
@Marc
Yes i have put 666 for the right
And Filezilla have deleted this pretentious file without problem :wink:
With Curl W10 i have a certificate problem :|
Cuel W10 wrote:curl: (35) schannel: next InitializeSecurityContext failed: CRYPT_E_REVOKED (0x80092010) - Le certificat est révoqué.
@FRED
I have try with

Code: Select all

*headerlist = curl_slist_append(*headerlist, "RM " + #REMOTE_FILE)
and

Code: Select all

curl_easy_setopt_str(curl, #CURLOPT_CUSTOMREQUEST, "RM " + #REMOTE_FILE)
and

Code: Select all

*headerlist = curl_slist_append(*headerlist, "rm " + #REMOTE_FILE)
and

Code: Select all

curl_easy_setopt_str(curl, #CURLOPT_CUSTOMREQUEST, "rm " + #REMOTE_FILE)
I have OK answer, but the file starts to taunt me again :cry:

Re: LibCurl delete a remote file

Posted: Sat Aug 10, 2024 11:28 pm
by infratec
Try this:

Code: Select all

; https://www.purebasic.fr/english/viewtopic.php?p=562174#p562174

; SFTP delete file

EnableExplicit

#LibCurl_ExternalDLL = #True
IncludeFile "libcurl.pbi"

#REMOTE_URL$ = "sftp://server"
#Username$ = "user"
#Password$ = "password"
#FileToDelete$ = "/full/path/to/the/file"

Define curl.i, res.i
Define *cmdlist
Define UserData.libcurl_userdata_structure

CompilerIf #PB_Compiler_Version < 600
  InitNetwork()
CompilerEndIf

curl_global_init(#CURL_GLOBAL_DEFAULT)

curl = curl_easy_init()
If curl
  
  curl_easy_setopt_str(curl, #CURLOPT_URL, #REMOTE_URL$)
  
  curl_easy_setopt_str(curl, #CURLOPT_USERNAME, #Username$)
  curl_easy_setopt_str(curl, #CURLOPT_PASSWORD, #Password$)
  
  curl_easy_setopt(curl, #CURLOPT_FTP_USE_EPSV, 0)
  
  curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @LibCurl_WriteFunction())
  curl_easy_setopt(curl, #CURLOPT_WRITEDATA, @UserData)
  
  curl_easy_setopt(curl, #CURLOPT_USE_SSL, #CURLUSESSL_ALL)
  curl_easy_setopt(curl, #CURLOPT_FTPSSLAUTH, #CURLFTPAUTH_TLS)
  
  ; accept insecure certificates
  curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, #False)
  curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, #False)

  *cmdlist = curl_slist_append(#Null, "RM " + #FileToDelete$)
  curl_easy_setopt(curl, #CURLOPT_QUOTE, *cmdlist)
  
  ; if no body (listing) is wanted
  curl_easy_setopt(curl, #CURLOPT_NOBODY, 1)
  
  curl_easy_setopt(curl, #CURLOPT_VERBOSE, #True)
  
  res = curl_easy_perform(curl)
  If res = #CURLE_OK
    Debug "Ok"
    If UserData\Memory
      Debug PeekS(UserData\Memory, MemorySize(UserData\Memory), #PB_UTF8|#PB_ByteLength)
    EndIf
  Else
    Debug "Error: " + curl_easy_strerror(res)
  EndIf
  
  curl_slist_free_all(*cmdlist)
  
  curl_easy_cleanup(curl)
  
Else
  MessageRequester("Error", "Was not able to init curl")
EndIf

curl_global_cleanup()

Re: LibCurl delete a remote file

Posted: Sun Aug 11, 2024 9:35 am
by infratec
The example above works with ftps.

sftp is more like ssh you have to use cd and rm as commands.

FTP(s):
https://www.microfocus.com/documentatio ... index.html

SFTP:
https://www.microfocus.com/documentatio ... index.html

Re: LibCurl delete a remote file

Posted: Sun Aug 11, 2024 9:48 am
by Kwai chang caine
Hello Master,

I have try with CD/RM and CWD/DELE without succes
I have always this answer of QUOTE :|

Re: LibCurl delete a remote file

Posted: Sun Aug 11, 2024 11:43 am
by infratec
I published examples in the libcurl zip file.

Re: LibCurl delete a remote file

Posted: Sun Aug 11, 2024 12:24 pm
by Kwai chang caine
One thousand of thanks MASTER
Have a very good day 8)

Re: LibCurl delete a remote file [Greatly resolved]

Posted: Thu Oct 31, 2024 6:07 pm
by taoalu
I'm facing the same problem, where can I get the zip with the examples?

Re: LibCurl delete a remote file [Greatly resolved]

Posted: Thu Oct 31, 2024 6:19 pm
by infratec