2025-10-08
admin
Diperbarui 2025-10-11

Tutorial: Testing NEO Storage API dengan cURL untuk Pemula

Ingin menguji NEO Storage API dengan cURL? Artikel ini memberikan panduan praktis, mulai dari persiapan API key hingga contoh script lengkap untuk upload, list, dan delete file. Dijamin berhasil!
Tutorial: Testing NEO Storage API dengan cURL untuk Pemula

🧪 Testing NEO Storage API dengan cURL

📋 Persiapan

1. Dapatkan API Key

  1. Login ke http://localhost:7600
  2. Menu API Keys → Buat API key baru
  3. Klik tombol 👁️ (eye) untuk lihat full key
  4. Copy API key lengkap

2. Set Environment Variable (Opsional)

export API_KEY="your-very-long-api-key-here"
export BASE_URL="http://localhost:7600"

🔌 Testing Endpoints

1. API Info (Tanpa Auth)

curl http://localhost:7600/api/info

Response:

{
  "service": "NEO Storage API - IndoWeb ID",
  "version": "1.0.0",
  "endpoints": [
    "POST /api/upload - Upload file",
    "GET /api/files - List files",
    "POST /api/delete - Delete file"
  ],
  "authentication": "X-API-Key header required"
}

2. Upload File (Image)

Cara 1: Manual (paste API key langsung)

curl -X POST http://localhost:7600/api/upload \
  -H "X-API-Key: PASTE_YOUR_API_KEY_HERE" \
  -F "file=@/path/to/image.jpg" \
  -F "folder=uploads"

Cara 2: Menggunakan Variable

curl -X POST $BASE_URL/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "[email protected]" \
  -F "folder=test_uploads"

Cara 3: Upload dengan Pretty Print

curl -X POST http://localhost:7600/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "[email protected]" \
  -F "folder=presensi" | python3 -m json.tool

Response Success:

{
  "success": true,
  "url": "http://neo-endpoint/bucket/uploads/20251005_143022_abc123_image.jpg",
  "filename": "20251005_143022_abc123_image.jpg",
  "object_key": "uploads/20251005_143022_abc123_image.jpg"
}

Response Error (Invalid API Key):

{
  "status": "error",
  "message": "Invalid or inactive API key"
}

3. Upload dengan Custom Folder

# Upload ke folder presensi/januari/2025
curl -X POST http://localhost:7600/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "file=@foto_karyawan.jpg" \
  -F "folder=presensi/januari/2025"

4. List Files

List semua file di folder default:

curl -X GET "http://localhost:7600/api/files?folder=uploads" \
  -H "X-API-Key: $API_KEY"

List file di folder spesifik:

curl -X GET "http://localhost:7600/api/files?folder=presensi/januari/2025" \
  -H "X-API-Key: $API_KEY" | python3 -m json.tool

Response:

{
  "success": true,
  "files": [
    {
      "key": "uploads/file.jpg",
      "filename": "file.jpg",
      "size": 245670,
      "modified": "2025-10-05 14:30:22",
      "url": "http://neo-endpoint/bucket/uploads/file.jpg"
    }
  ]
}

5. Delete File

curl -X POST http://localhost:7600/api/delete \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "object_key": "uploads/20251005_143022_abc123_image.jpg"
  }'

Response:

{
  "success": true
}

📝 Testing Script Lengkap

Bash Script untuk Testing

Buat file test_api.sh:

#!/bin/bash

# Configuration
API_KEY="paste-your-api-key-here"
BASE_URL="http://localhost:7600"

echo "🧪 NEO Storage API Testing"
echo "=========================="

# 1. Test API Info
echo -e "\n1️⃣ Testing API Info..."
curl -s $BASE_URL/api/info | python3 -m json.tool

# 2. Upload file
echo -e "\n2️⃣ Uploading test file..."
UPLOAD_RESULT=$(curl -s -X POST $BASE_URL/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "[email protected]" \
  -F "folder=test_folder")

echo $UPLOAD_RESULT | python3 -m json.tool

# Extract object_key from response
OBJECT_KEY=$(echo $UPLOAD_RESULT | python3 -c "import sys, json; print(json.load(sys.stdin).get('object_key', ''))")

# 3. List files
echo -e "\n3️⃣ Listing files..."
curl -s -X GET "$BASE_URL/api/files?folder=test_folder" \
  -H "X-API-Key: $API_KEY" | python3 -m json.tool

# 4. Delete file
if [ ! -z "$OBJECT_KEY" ]; then
    echo -e "\n4️⃣ Deleting file: $OBJECT_KEY"
    curl -s -X POST $BASE_URL/api/delete \
      -H "X-API-Key: $API_KEY" \
      -H "Content-Type: application/json" \
      -d "{\"object_key\": \"$OBJECT_KEY\"}" | python3 -m json.tool
fi

echo -e "\n✅ Testing completed!"

Jalankan:

chmod +x test_api.sh
./test_api.sh

🐍 Python Testing Script

Buat file test_api.py:

#!/usr/bin/env python3
import requests
import json

# Configuration
API_KEY = "paste-your-api-key-here"
BASE_URL = "http://localhost:7600"
HEADERS = {"X-API-Key": API_KEY}

def test_api_info():
    print("1️⃣ Testing API Info...")
    r = requests.get(f"{BASE_URL}/api/info")
    print(json.dumps(r.json(), indent=2))

def test_upload():
    print("\n2️⃣ Testing Upload...")
    with open("test.jpg", "rb") as f:
        files = {"file": f}
        data = {"folder": "python_test"}
        r = requests.post(f"{BASE_URL}/api/upload", 
                         headers=HEADERS, files=files, data=data)
        result = r.json()
        print(json.dumps(result, indent=2))
        return result.get("object_key")

def test_list_files():
    print("\n3️⃣ Testing List Files...")
    r = requests.get(f"{BASE_URL}/api/files", 
                     headers=HEADERS, params={"folder": "python_test"})
    print(json.dumps(r.json(), indent=2))

def test_delete(object_key):
    print(f"\n4️⃣ Testing Delete: {object_key}")
    r = requests.post(f"{BASE_URL}/api/delete",
                     headers=HEADERS, json={"object_key": object_key})
    print(json.dumps(r.json(), indent=2))

if __name__ == "__main__":
    print("🧪 NEO Storage API Testing\n" + "="*40)
    test_api_info()
    object_key = test_upload()
    test_list_files()
    if object_key:
        test_delete(object_key)
    print("\n✅ Testing completed!")

Jalankan:

python3 test_api.py

🔍 Troubleshooting

Error: "Invalid or inactive API key"

# Pastikan API key benar (copy dari web UI)
# Test dengan verbose mode untuk lihat request
curl -v -X POST http://localhost:7600/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "[email protected]"

Error: "No file provided"

# Pastikan path file benar dan file exists
ls -lh test.jpg
curl -X POST http://localhost:7600/api/upload \
  -H "X-API-Key: $API_KEY" \
  -F "file=@./test.jpg"  # gunakan ./ jika file di current directory

Error: Connection refused

# Pastikan aplikasi running
curl http://localhost:7600/api/info

# Check port
netstat -tuln | grep 7600

📊 Monitoring Request di Terminal

Buka terminal kedua dan jalankan:

# Follow application logs
tail -f /path/to/app.log

# Atau watch database logs
watch -n 1 'sqlite3 neo_storage.db "SELECT * FROM access_log ORDER BY id DESC LIMIT 5"'

✅ Quick Test Command

One-liner test:

curl -X POST http://localhost:7600/api/upload -H "X-API-Key: YOUR_KEY" -F "[email protected]" -F "folder=quick_test" && echo "\n✅ Upload success!"

Check if API key works:

curl -X GET http://localhost:7600/api/files -H "X-API-Key: YOUR_KEY" && echo "\n✅ API Key valid!"

Selamat Testing! 🚀

blog teknologi programming admin

Artikel Terkait