Copiar
// Usando Fetch API (Moderno)
fetch('https://api.exemplo.com/v1/resource', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token'
},
body: JSON.stringify({
chave: 'valor'
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Erro:', error));
Copiar
import requests
url = 'https://api.exemplo.com/v1/resource'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer token'
}
data = {
'chave': 'valor'
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.json())
Copiar
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String jsonBody = "{\"chave\":\"valor\"}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.exemplo.com/v1/resource"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer token")
.POST(BodyPublishers.ofString(jsonBody))
.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
Copiar
using System.Net.Http;
using System.Text;
using System.Text.Json;
var client = new HttpClient();
var url = "https://api.exemplo.com/v1/resource";
var data = new { chave = "valor" };
var json = JsonSerializer.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Bearer token");
var response = await client.PostAsync(url, content);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
Copiar
package main
import (
"bytes"
"encoding/json"
"net/http"
"fmt"
)
func main() {
url := "https://api.exemplo.com/v1/resource"
data := map[string]string{"chave": "valor"}
jsonData, _ := json.Marshal(data)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.Status)
}
Copiar
$url = "https://api.exemplo.com/v1/resource";
$data = array("chave" => "valor");
$json_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer token'
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Copiar
require 'net/http'
require 'json'
require 'uri'
uri = URI('https://api.exemplo.com/v1/resource')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path, {
'Content-Type' => 'application/json',
'Authorization' => 'Bearer token'
})
request.body = { chave: 'valor' }.to_json
response = http.request(request)
puts response.body