POST metodu
1. header("Content-Type", "application/json") yapmak gerekir.
2. POST() çağrısına JSON string geçilir
Örnek
Şöyle yaparız
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"key\": \"value\"}"))
.build();
Örnek
Şöyle yaparız
HttpRequest requestBody = HttpRequest.newBuilder()
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\": \"morpheus\",
\"job\": \"leader\"}"))
.uri(URI.create("https://reqres.in/api/users"))
.build();
ÖrnekŞöyle yaparız.
HttpResponse<String> postData(String jsonStr, String endpoint, String accessToken) throws Exception { HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest httpRequest = HttpRequest.newBuilder() .header("Content-Type", "application/json") .header("Authorization", "Bearer " + accessToken) .uri(URI.create("https://...")) .POST(HttpRequest.BodyPublishers.ofString(jsonStr)) .build(); ... }
Şöyle yaparız.
HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(basePhoneHomeUrl)).timeout(Duration.ofMillis(TIMEOUT)).POST(HttpRequest.BodyPublishers.ofString(requestBody, StandardCharsets.UTF_8)).build();
Hiç yorum yok:
Yorum Gönder