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.
50-http-post.nxFuente →
// HTTP POST request with a body // Solicitud HTTP POST con cuerpo import "std/http" fn main() -> int { // http_post sends a POST with Content-Type: text/plain 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) } // For custom headers and methods, use 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 ilustrativastdout
status: 200
body (first 200 chars): { "args": {}, "data": "hello from Nyx", ...}
PUT status: 200Cómo funciona
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.