LibCurl delete a remote file [Greatly resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

LibCurl delete a remote file [Greatly resolved]

Post 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
Last edited by Kwai chang caine on Sun Aug 11, 2024 12:25 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: LibCurl delete a remote file

Post by Marc56us »

Try add -X

Code: Select all

curl -X DELETE [URL]
(not tested)
:wink:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: LibCurl delete a remote file

Post 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:
ImageThe happiness is a road...
Not a destination
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: LibCurl delete a remote file

Post by Marc56us »

-X
(I don't if case sensitive ?)
?
curl_easy_setopt_str(*HTTPRequest\curl, #CURLOPT_CUSTOMREQUEST, "-X DELETE")
Not tested
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: LibCurl delete a remote file

Post 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 :|
ImageThe happiness is a road...
Not a destination
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: LibCurl delete a remote file

Post 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)
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: LibCurl delete a remote file

Post by Fred »

Try 'rm yourfilename'
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: LibCurl delete a remote file

Post 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:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LibCurl delete a remote file

Post 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()
Last edited by infratec on Sun Aug 11, 2024 4:59 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LibCurl delete a remote file

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: LibCurl delete a remote file

Post by Kwai chang caine »

Hello Master,

I have try with CD/RM and CWD/DELE without succes
I have always this answer of QUOTE :|
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LibCurl delete a remote file

Post by infratec »

I published examples in the libcurl zip file.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5500
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: LibCurl delete a remote file

Post by Kwai chang caine »

One thousand of thanks MASTER
Have a very good day 8)
ImageThe happiness is a road...
Not a destination
taoalu
New User
New User
Posts: 1
Joined: Thu Oct 31, 2024 6:05 pm

Re: LibCurl delete a remote file [Greatly resolved]

Post by taoalu »

I'm facing the same problem, where can I get the zip with the examples?
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LibCurl delete a remote file [Greatly resolved]

Post by infratec »

Post Reply