Skip to content

Commit e8bd81c

Browse files
committed
Add a function to allow a raw post instead of a form post to allow for e.g. JSON
1 parent 77be962 commit e8bd81c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/BizHawk.Client.Common/Api/HttpCommunication.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Net.Http;
3+
using System.Text;
34
using System.Threading.Tasks;
45

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

41+
public string ExecPostRaw(string url = null, string payload = "", string contentType = "text/plain") => Post(
42+
url ?? PostUrl,
43+
new StringContent(payload, Encoding.UTF8, contentType)
44+
).Result;
45+
4046
public async Task<string> Get(string url)
4147
{
4248
_client.DefaultRequestHeaders.ConnectionClose = false;
@@ -48,7 +54,7 @@ public async Task<string> Get(string url)
4854
return null;
4955
}
5056

51-
public async Task<string> Post(string url, FormUrlEncodedContent content)
57+
public async Task<string> Post(string url, HttpContent content)
5258
{
5359
_client.DefaultRequestHeaders.ConnectionClose = true;
5460
_client.DefaultRequestHeaders.ExpectContinue = ExpectContinue;

src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,20 @@ public string HttpGet(string url)
196196
return APIs.Comm.HTTP?.ExecGet(url);
197197
}
198198

199-
[LuaMethod("httpPost", "makes a HTTP POST request")]
199+
[LuaMethod("httpPost", "makes a HTTP POST request with the payload form encoded as the 'payload' field")]
200200
public string HttpPost(string url, string payload)
201201
{
202202
CheckHttp();
203203
return APIs.Comm.HTTP?.ExecPost(url, payload);
204204
}
205205

206+
[LuaMethod("httpPostRaw", "makes a HTTP POST request with the supplied payload sent directly as the body")]
207+
public string HttpPostRaw(string url, string payload, string contentType)
208+
{
209+
CheckHttp();
210+
return APIs.Comm.HTTP?.ExecPostRaw(url, payload, contentType);
211+
}
212+
206213
[LuaMethod("httpPostScreenshot", "HTTP POST screenshot")]
207214
public string HttpPostScreenshot()
208215
{

0 commit comments

Comments
 (0)