Request Types - Nexa Documentation

Request Types

Format Body Request

API mendukung beberapa format body request yang umum digunakan:

1. JSON (application/json)

Contoh Request dengan curl:

curl -X POST http://localhost/api/Info \
  -H "Content-Type: application/json" \
  -d '{"name":"test","type":"info"}'

Contoh Data:

{
    "name": "test",
    "type": "info",
    "attributes": {
        "key1": "value1",
        "key2": "value2"
    }
}

2. Form Data (multipart/form-data)

Contoh Request dengan curl:

curl -X POST http://localhost/api/Info \
  -F "name=test" \
  -F "type=info" \
  -F "image=@file.jpg"

Cocok untuk:

  • Upload file
  • Data form sederhana
  • Kombinasi file dan data

3. URL Encoded (application/x-www-form-urlencoded)

Contoh Request dengan curl:

curl -X POST http://localhost/api/Info \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "name=test&type=info"

Cocok untuk:

  • Form submission sederhana
  • Data key-value pairs
  • Browser-native form submissions

Best Practices

  • Gunakan JSON untuk API modern dan data terstruktur
  • Gunakan Form Data untuk upload file
  • Gunakan URL Encoded untuk form submission sederhana
  • Selalu sertakan header Content-Type yang sesuai
  • Validasi format input di sisi server

Contoh Implementasi

JavaScript Fetch API:

// JSON Request
fetch('/api/Info', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        name: 'test',
        type: 'info'
    })
})

// Form Data Request
const formData = new FormData();
formData.append('name', 'test');
formData.append('type', 'info');
formData.append('file', fileInput.files[0]);

fetch('/api/Info', {
    method: 'POST',
    body: formData
})