ClamAV Antivirus API Server Code Examples (Google Cloud)
curl -d '{"stream": true, "bucket": "elm-bucket-virus-scan", "object": "test/virus_test.txt"}' -H 'Content-Type: application/json' http://172.31.39.37:8080/api/v1/gcp/scanPython
Section titled “Python”import requestsimport json
def elm_scan_object(url, stream, bucket, object): # Define the payload as a dictionary payload = { "stream": stream, "bucket": bucket, "object": object }
# Set the headers headers = { "Content-Type": "application/json" }
# Send a POST request to the API endpoint response = requests.post(url, data=json.dumps(payload), headers=headers)
# Check the response status code if response.status_code == 200: # Request was successful data = response.json() # Extract the JSON data from the response return data else: # Request was not successful print(f"Request failed with status code: {response.status_code}") return None
if __name__ == '__main__': result = elm_scan_object( url = "http://172.31.39.37:8080/api/v1/gcp/scan", \ stream = True, \ bucket = 'elm-bucket-virus-scan', \ object = 'virus_test.txt')
if result: print(result) print(result['code']) print(result['message'])JavaScript
Section titled “JavaScript”const axios = require('axios');
async function elmScanObject(url, stream, bucket, object) { try { const payload = { stream, bucket, object, };
const headers = { 'Content-Type': 'application/json', };
const response = await axios.post(url, payload, { headers });
if (response.status === 200) { return response.data; } else { console.log(`Request failed with status code: ${response.status}`); return null; } } catch (error) { console.error('An error occurred:', error.message); return null; }}
async function main() { const url = 'http://172.31.39.37:8080/api/v1/gcp/scan'; const stream = true; const bucket = 'elm-bucket-virus-scan'; const object = 'virus_test.txt';
const result = await elmScanObject(url, stream, bucket, object);
if (result) { console.log(result); console.log(result.code); console.log(result.message); }}
main();import 'dart:convert';import 'package:http/http.dart' as http;
Future<Map<String, dynamic>?> elmScanObject(String url, bool stream, String bucket, String object) async { try { final payload = { 'stream': stream, 'bucket': bucket, 'object': object, };
final headers = { 'Content-Type': 'application/json', };
final response = await http.post(Uri.parse(url), headers: headers, body: jsonEncode(payload));
if (response.statusCode == 200) { return jsonDecode(response.body); } else { print('Request failed with status code: ${response.statusCode}'); return null; } } catch (error) { print('An error occurred: $error'); return null; }}
void main() async { final url = 'http://172.31.39.37:8080/api/v1/gcp/scan'; final stream = true; final bucket = 'elm-bucket-virus-scan'; final object = 'virus_test.txt';
final result = await elmScanObject(url, stream, bucket, object);
if (result != null) { print(result); print(result['code']); print(result['message']); }}package main
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http")
func elmScanObject(url string, stream bool, bucket string, object string) (map[string]interface{}, error) { payload := map[string]interface{}{ "stream": stream, "bucket": bucket, "object": object, }
payloadBytes, err := json.Marshal(payload) if err != nil { return nil, err }
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes)) if err != nil { return nil, err }
req.Header.Set("Content-Type", "application/json")
client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err }
if resp.StatusCode == http.StatusOK { var data map[string]interface{} err = json.Unmarshal(respBody, &data) if err != nil { return nil, err } return data, nil } else { return nil, fmt.Errorf("Request failed with status code: %d", resp.StatusCode) }}
func main() { url := "http://172.31.39.37:8080/api/v1/gcp/scan" stream := true bucket := "elm-bucket-virus-scan" object := "virus_test.txt"
result, err := elmScanObject(url, stream, bucket, object) if err != nil { fmt.Println("An error occurred:", err) return }
if result != nil { fmt.Println(result) fmt.Println(result["code"]) fmt.Println(result["message"]) }}Scan an uploaded file
Section titled “Scan an uploaded file”To scan file bytes directly (without staging the file in a GCS bucket), send the
raw file as the request body to /api/v1/scan/upload. The response includes the
scanned size_bytes.
curl --data-binary @/tmp/test.pdf -H 'Content-Type: application/octet-stream' \ http://172.31.39.37:8080/api/v1/scan/uploadimport requests
with open("/tmp/test.pdf", "rb") as f: response = requests.post( "http://172.31.39.37:8080/api/v1/scan/upload", data=f, headers={"Content-Type": "application/octet-stream"}, )
print(response.json()) # {"code": 0, "message": "OK", "size_bytes": 12345}