Skip to content

Commit

Permalink
Add a function to allow a raw post instead of a form post to allow fo…
Browse files Browse the repository at this point in the history
…r e.g. JSON
  • Loading branch information
reversefold committed Jan 15, 2025
1 parent 77be962 commit e8bd81c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/BizHawk.Client.Common/Api/HttpCommunication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using BizHawk.Common;
Expand Down Expand Up @@ -37,6 +38,11 @@ public string ExecPost(string url = null, string payload = "") => Post(
new FormUrlEncodedContent(new Dictionary<string, string> { ["payload"] = payload })
).Result;

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<string> Get(string url)
{
_client.DefaultRequestHeaders.ConnectionClose = false;
Expand All @@ -48,7 +54,7 @@ public async Task<string> Get(string url)
return null;
}

public async Task<string> Post(string url, FormUrlEncodedContent content)
public async Task<string> Post(string url, HttpContent content)
{
_client.DefaultRequestHeaders.ConnectionClose = true;
_client.DefaultRequestHeaders.ExpectContinue = ExpectContinue;
Expand Down
9 changes: 8 additions & 1 deletion src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?.ExecPost(url, 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()
{
Expand Down

0 comments on commit e8bd81c

Please sign in to comment.