HTTP POST
http_post(url, body) envía una solicitud POST con Content-Type: text/plain. Para encabezados y métodos personalizados, usa http_request(method, url, headers, body), que da control total sobre la solicitud.
Código
// HTTP POST request with a body
import "std/http"
fn main() -> int {
let resp: Array = http_post("http://httpbin.org/post", "hello from Nyx")
let status: int = http_status(resp)
print("status: " + int_to_string(status))
let body: String = http_body(resp)
if body.length() > 200 {
print("body (first 200 chars): " + body.substring(0, 200))
} else {
print("body: " + body)
}
// Custom headers and methods with http_request
let headers: Array = ["Content-Type", "application/json"]
let json_body: String = "{\"key\": \"value\"}"
let resp2: Array = http_request("PUT", "http://httpbin.org/put", headers, json_body)
print("PUT status: " + int_to_string(http_status(resp2)))
return 0
}
Salida
status: 200
body (first 200 chars): { "args": {}, "data": "hello from Nyx", ...}
PUT status: 200
Explicación
http_post es un envoltorio de conveniencia que fija Content-Type: text/plain. Para APIs JSON, usa http_request con encabezados explícitos — el array de encabezados usa un formato plano clave-valor: ["Header-Name", "value", "Another", "value"].
http_request admite cualquier método HTTP (GET, POST, PUT, DELETE, PATCH, etc.) y da control total sobre los encabezados y el contenido del cuerpo.