From 329ac70a322846aa104d68ae992c3fd9fe63c72b Mon Sep 17 00:00:00 2001 From: "emrah.tokalak" Date: Tue, 13 Jun 2023 00:49:43 +0300 Subject: [PATCH 1/4] Fix: #5533 - Enabled reading request body for HTTP POST requests. --- .../Controllers/ApiController.cs | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs index c453a6af237..c8b2733def8 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs @@ -1,3 +1,5 @@ +using System.Text; +using System.IO; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; @@ -25,9 +27,7 @@ IQueryManager queryManager [HttpPost, HttpGet] [Route("{name}")] - public async Task Query( - string name, - string parameters) + public async Task Query(string name) { var query = await _queryManager.GetQueryAsync(name); @@ -43,12 +43,30 @@ public async Task Query( return NotFound(); } - var queryParameters = parameters != null ? + string parameters; + if (Request.Method == "GET") + { + parameters = Request.Query["parameters"].ToString(); + } + else if (Request.Method == "POST") + { + using (var reader = new StreamReader(Request.Body, Encoding.UTF8)) + { + parameters = await reader.ReadToEndAsync(); + } + } + else + { + return BadRequest("Invalid request method"); + } + + var queryParameters = !string.IsNullOrEmpty(parameters) ? JsonConvert.DeserializeObject>(parameters) : new Dictionary(); var result = await _queryManager.ExecuteQueryAsync(query, queryParameters); return new ObjectResult(result); } + } } From 3fe8a38a4f89699bfa141c1d6083faaaa46b5565 Mon Sep 17 00:00:00 2001 From: "emrah.tokalak" Date: Mon, 7 Aug 2023 01:10:00 +0300 Subject: [PATCH 2/4] fix: POST type, parameters query string reading. --- .../Controllers/ApiController.cs | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs index c8b2733def8..ce4fd009041 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; +using System; + namespace OrchardCore.Queries.Controllers { @@ -27,7 +29,9 @@ IQueryManager queryManager [HttpPost, HttpGet] [Route("{name}")] - public async Task Query(string name) + public async Task Query( + string name, + string parameters) { var query = await _queryManager.GetQueryAsync(name); @@ -43,30 +47,21 @@ public async Task Query(string name) return NotFound(); } - string parameters; - if (Request.Method == "GET") - { - parameters = Request.Query["parameters"].ToString(); - } - else if (Request.Method == "POST") + if (Request.Method == "POST") { - using (var reader = new StreamReader(Request.Body, Encoding.UTF8)) + if (String.IsNullOrEmpty(parameters)) { + using var reader = new StreamReader(Request.Body, Encoding.UTF8); parameters = await reader.ReadToEndAsync(); } } - else - { - return BadRequest("Invalid request method"); - } - var queryParameters = !string.IsNullOrEmpty(parameters) ? + var queryParameters = !String.IsNullOrEmpty(parameters) ? JsonConvert.DeserializeObject>(parameters) : new Dictionary(); var result = await _queryManager.ExecuteQueryAsync(query, queryParameters); return new ObjectResult(result); } - } } From c0b9122151a440d78767022793a3105250b9f860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Ros?= Date: Fri, 2 Feb 2024 14:57:15 -0800 Subject: [PATCH 3/4] Update ApiController.cs --- .../OrchardCore.Queries/Controllers/ApiController.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs index 8929b567112..726837b2494 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs @@ -1,6 +1,7 @@ -using System.Text; -using System.IO; using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; @@ -45,13 +46,13 @@ public async Task Query( return NotFound(); } - if (Request.Method == "POST" && string.IsNullOrEmpty(parameters)) + if (Request.Method == HttpMethods.Post && string.IsNullOrEmpty(parameters)) { using var reader = new StreamReader(Request.Body, Encoding.UTF8); parameters = await reader.ReadToEndAsync(); } - var queryParameters = !String.IsNullOrEmpty(parameters) ? + var queryParameters = !string.IsNullOrEmpty(parameters) ? JConvert.DeserializeObject>(parameters) : []; From f25d45e7d8697cc0672057daa2382d8a37e5ff64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Ros?= Date: Fri, 2 Feb 2024 15:13:58 -0800 Subject: [PATCH 4/4] Update ApiController.cs --- .../OrchardCore.Queries/Controllers/ApiController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs index 726837b2494..94733b1e6b2 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/ApiController.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.IO; -using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace OrchardCore.Queries.Controllers