diff --git a/Examples/Nodify.Calculator/Execution/Handlers/NodeHandlers.cs b/Examples/Nodify.Calculator/Execution/Handlers/NodeHandlers.cs index d2fd721..c38ba22 100644 --- a/Examples/Nodify.Calculator/Execution/Handlers/NodeHandlers.cs +++ b/Examples/Nodify.Calculator/Execution/Handlers/NodeHandlers.cs @@ -683,7 +683,19 @@ namespace Nodify.Calculator.Execution.Handlers ? apiVm.OperationType?.ToLower() ?? "get" : "get"; - var res = ctx.Executor.GetResponsePublic(url, httpMethod); + // Read the request body if present (POST/PUT/PATCH have a Body input — Square/Grid) + string body = null; + var bodyInput = node.Input.FirstOrDefault(i => + (i.Title ?? "").StartsWith("Body", StringComparison.OrdinalIgnoreCase) + && i.Shape != ConnectorShape.Triangle); + if (bodyInput != null) + { + body = ctx.ReadInput(bodyInput); + if (!string.IsNullOrWhiteSpace(body)) + ctx.Log($"[API] Request body: {(body.Length > 150 ? body.Substring(0, 150) + "..." : body)}"); + } + + var res = ctx.Executor.GetResponsePublic(url, httpMethod, body); if (!string.IsNullOrEmpty(res)) { ctx.Outputs[node.NodeId] = res; diff --git a/Examples/Nodify.Calculator/Executor.cs b/Examples/Nodify.Calculator/Executor.cs index cd7d41e..e440860 100644 --- a/Examples/Nodify.Calculator/Executor.cs +++ b/Examples/Nodify.Calculator/Executor.cs @@ -509,7 +509,8 @@ namespace Nodify.Calculator internal void ExecuteFunctionPublic(FunctionOperationViewModel funcOp, ICollection connections) => ExecuteFunction(funcOp, connections); - internal string GetResponsePublic(string url, string type) => GetResponse(url, type); + internal string GetResponsePublic(string url, string type) => GetResponse(url, type, null); + internal string GetResponsePublic(string url, string type, string body) => GetResponse(url, type, body); internal bool TraverseChainPublic(OperationViewModel node, string endNodeTitle, ICollection connections, @@ -716,21 +717,59 @@ namespace Nodify.Calculator OnLogMe?.Invoke($"Function '{funcOp.FunctionName}' execution completed."); } - private string GetResponse(string url, string type) + private string GetResponse(string url, string type, string body) { string baseURL = !string.IsNullOrWhiteSpace(_authBaseUrl) ? _authBaseUrl : "https://localhost:7107"; string responseString = string.Empty; #if NET8_0_OR_GREATER - if (string.Equals(type, "get", StringComparison.OrdinalIgnoreCase)) + var fullUri = new Uri(new Uri(baseURL.EndsWith("/") ? baseURL : baseURL + "/"), url); + var methodLower = (type ?? "get").Trim().ToLowerInvariant(); + HttpMethod httpMethod = methodLower switch { - var fullUri = new Uri(new Uri(baseURL.EndsWith("/") ? baseURL : baseURL + "/"), url); - using var request = new HttpRequestMessage(HttpMethod.Get, fullUri); + "post" => HttpMethod.Post, + "put" => HttpMethod.Put, + "patch" => new HttpMethod("PATCH"), + "delete" => HttpMethod.Delete, + _ => HttpMethod.Get + }; + + try + { + using var request = new HttpRequestMessage(httpMethod, fullUri); ApplyAuthHeaders(request); + + // Attach body for write methods + if (methodLower != "get" && methodLower != "delete" && !string.IsNullOrWhiteSpace(body)) + { + request.Content = new StringContent(body, Encoding.UTF8, "application/json"); + } + else if (methodLower == "delete" && !string.IsNullOrWhiteSpace(body)) + { + // DELETE may optionally carry a body + request.Content = new StringContent(body, Encoding.UTF8, "application/json"); + } + + OnLogMe?.Invoke($"[HTTP] {httpMethod.Method} {fullUri}"); + if (!string.IsNullOrWhiteSpace(body)) + OnLogMe?.Invoke($"[HTTP] Body: {(body.Length > 200 ? body.Substring(0, 200) + "..." : body)}"); + using var response = _httpClient.Send(request); - response.EnsureSuccessStatusCode(); using var reader = new StreamReader(response.Content.ReadAsStream()); responseString = reader.ReadToEnd(); + + if (!response.IsSuccessStatusCode) + { + OnLogMe?.Invoke($"[HTTP] {(int)response.StatusCode} {response.StatusCode} — {responseString}", logType.Error); + } + else + { + OnLogMe?.Invoke($"[HTTP] {(int)response.StatusCode} {response.StatusCode}"); + } + } + catch (Exception ex) + { + OnLogMe?.Invoke($"[HTTP] Request failed: {ex.Message}", logType.Error); } #endif