Skip to content

Instantly share code, notes, and snippets.

@Yizack
Last active May 15, 2019 22:33
Show Gist options
  • Save Yizack/838e2a6e9d1b17e64663289db2742f06 to your computer and use it in GitHub Desktop.
Save Yizack/838e2a6e9d1b17e64663289db2742f06 to your computer and use it in GitHub Desktop.
Cloudflare purge files method (Visual Basic.NET)
' Title: Cloudflare.vb
' Author: @Yizack
' Description: Visual Basic class for purge files programmatically with the Cloudflare API.
' Date: 2019/05/15
Imports System.Net
Imports System.Text
Public Class Cloudflare
' Example use:
' PurgeFile("https://wwww.example.com/image/cat.jpg", "email@example.com", "YOUR API KEY", "YOUR ZONE KEY")
' Example use on another class:
' Cloudflare.PurgeFile("https://wwww.example.com/image/cat.jpg", "email@example.com", "YOUR API KEY", "YOUR ZONE KEY")
' Console success output: {"result":{"id":"YOUR ZONE KEY"},"success":true,"errors":[],"messages":[]}
Public Sub PurgeFile(ByVal FILE_URL As String, ByVal EMAIL As String,ByVal API_KEY As String, ByVal ZONE_KEY As String)
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim data As Byte() = Encoding.Default.GetBytes("{" & Chr(34) & "files" & Chr(34) & ":[" & Chr(34) & FILE_URL & Chr(34) & "]}")
Try
webClient.Headers("X-Auth-Email") = EMAIL
webClient.Headers("X-Auth-Key") = API_KEY
webClient.Headers("content-type") = "application/json"
resByte = webClient.UploadData("https://api.cloudflare.com/client/v4/zones/" & ZONE_KEY & "/purge_cache", "POST", data)
resString = Encoding.Default.GetString(resByte)
Console.WriteLine(resString)
webClient.Dispose()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment