From c40e0a776083f2432b8db3d486d95a7bb5440161 Mon Sep 17 00:00:00 2001 From: Julia Patrin Date: Tue, 14 Jan 2025 22:07:56 -0800 Subject: [PATCH 1/2] Add a function to allow a raw post instead of a form post to allow for e.g. JSON --- src/BizHawk.Client.Common/Api/HttpCommunication.cs | 6 ++++++ .../lua/CommonLibs/CommLuaLibrary.cs | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/BizHawk.Client.Common/Api/HttpCommunication.cs b/src/BizHawk.Client.Common/Api/HttpCommunication.cs index be64aec4bd6..0f30214696b 100644 --- a/src/BizHawk.Client.Common/Api/HttpCommunication.cs +++ b/src/BizHawk.Client.Common/Api/HttpCommunication.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Net.Http; +using System.Text; using System.Threading.Tasks; using BizHawk.Common; @@ -64,6 +65,11 @@ public string ExecPostAsForm(string url = null, string payload = "") } #pragma warning restore DOC105 + public string ExecPostRaw(string url = null, string payload = "", string contentType = "text/plain") => Post( + url ?? PostUrl, + new StringContent(payload, Encoding.UTF8, contentType) + ).Result; + public async Task Get(string url) { _client.DefaultRequestHeaders.ConnectionClose = false; diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs index 5d30328994e..cb48c3732ed 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs @@ -196,13 +196,20 @@ public string HttpGet(string url) return APIs.Comm.HTTP?.ExecGet(url); } - [LuaMethod("httpPost", "makes a HTTP POST request")] + [LuaMethod("httpPost", "makes a HTTP POST request with the payload form encoded as the 'payload' field")] public string HttpPost(string url, string payload) { CheckHttp(); return APIs.Comm.HTTP?.ExecPostAsForm(url: url, payload: payload); } + [LuaMethod("httpPostRaw", "makes a HTTP POST request with the supplied payload sent directly as the body")] + public string HttpPostRaw(string url, string payload, string contentType) + { + CheckHttp(); + return APIs.Comm.HTTP?.ExecPostRaw(url, payload, contentType); + } + [LuaMethod("httpPostScreenshot", "HTTP POST screenshot")] public string HttpPostScreenshot() { From 373ad08b2e604509a96dda5acb3a1908738d6b0d Mon Sep 17 00:00:00 2001 From: Julia Patrin Date: Wed, 15 Jan 2025 21:02:28 -0800 Subject: [PATCH 2/2] Set ExpectCompute based on the length of the payload --- src/BizHawk.Client.Common/Api/HttpCommunication.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/HttpCommunication.cs b/src/BizHawk.Client.Common/Api/HttpCommunication.cs index 0f30214696b..8e229fec2dc 100644 --- a/src/BizHawk.Client.Common/Api/HttpCommunication.cs +++ b/src/BizHawk.Client.Common/Api/HttpCommunication.cs @@ -65,10 +65,14 @@ public string ExecPostAsForm(string url = null, string payload = "") } #pragma warning restore DOC105 - public string ExecPostRaw(string url = null, string payload = "", string contentType = "text/plain") => Post( - url ?? PostUrl, - new StringContent(payload, Encoding.UTF8, contentType) - ).Result; + public string ExecPostRaw(string url = null, string payload = "", string contentType = "text/plain") + { + return Post( + url ?? PostUrl, + new StringContent(payload, Encoding.UTF8, contentType), + payload.Length > ExpectContinueThreshold + ).Result; + } public async Task Get(string url) {