CodeBlanks Logo Icon CodeBlanks

http.post

http.post ile veri göndermeyi öğrenirsin.

http.post nedir?

http.post sunucuya veri göndermek için kullanılan metoddur. headers ile içerik türünü, body ile gönderilecek veriyi belirlersin.

final response = await http.post(
  Uri.parse(url),
  headers: {'Content-Type': 'application/json'},
  body: '{"isim": "Ayşe"}',
);

POST genellikle yeni kayıt oluşturmak için kullanılır.

headers ve body

headers, isteğin ek bilgilerini taşır; örneğin Content-Type gövdenin formatını belirtir. body ise gönderilecek asıl veridir.

await http.post(
  uri,
  headers: {'Content-Type': 'application/json'},
  body: jsonEncode({'isim': 'Ayşe'}),
);

JSON verisi için Content-Type application/json olmalıdır.

Uygulama http.post ile veri gönder

veriGonder async metodu yaz. http.post çağrısında Uri.parse('https://api.example.com/kullanici') ver. headers: {'Content-Type': 'application/json'} ve body: '{"isim": "Ayşe"}' parametrelerini ekle. Sonra print('gönderildi: ${response.statusCode}') yaz. main'de metodu await et.

Başlangıç kodu

main.dart
import 'package:http/http.dart' as http;

// veriGonder: async metot ekle

void main() async {
  // veriGonder'i çağır
}

Örnek çözüm

main.dart
import 'package:http/http.dart' as http;

Future<void> veriGonder() async {
  final response = await http.post(
    Uri.parse('https://api.example.com/kullanici'),
    headers: {'Content-Type': 'application/json'},
    body: '{"isim": "Ayşe"}',
  );
  print('gönderildi: ${response.statusCode}');
}

void main() async {
  await veriGonder();
}

Beklenen çıktı

gönderildi: 201

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 http.post'a gönderecek veriyi hangi parametreyle verirsin?

  • uri
  • body Doğru cevap
  • headers
  • method

Açıklama: http.post'a gönderecek veriyi body parametresiyle verirsin; headers ise içerik türünü belirtir.

2 http.post'a headers parametresini ekle

Soru
final response = await http.post(
  uri,
  ____: {'Content-Type': 'application/json'},
  body: '{"isim": "Ayşe"}',
);
Cevap
final response = await http.post(
  uri,
  headers: {'Content-Type': 'application/json'},
  body: '{"isim": "Ayşe"}',
);

Açıklama: http.post'un headers parametresi içerik türü gibi ek bilgileri taşır; body ise gönderilecek veridir.

3 http.post parçaları

Kavram Anlamı
http.post Veri gönderme metodu
headers İçerik türü gibi ek bilgiler
body Gönderilecek asıl veri
Content-Type Gövde formatını belirten başlık

Orta seviye bölümündeki tüm dersler