Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comm.httpPostRaw #4189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/BizHawk.Client.Common/Api/HttpCommunication.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -64,6 +65,15 @@ public string ExecPostAsForm(string url = null, string payload = "")
}
#pragma warning restore DOC105

public string ExecPostRaw(string url = null, string payload = "", string contentType = "text/plain")
{
return Post(
url ?? PostUrl,
new StringContent(payload, Encoding.UTF8, contentType),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set up ContentObjectFor above for you—it should be simply

-#pragma warning disable BHI1005 // exception type
-				_ => throw new NotImplementedException()
+				/*MIME_PLAINTEXT,*/ _ => new StringContent(payload, Encoding.UTF8, mimeType)
-#pragma warning restore BHI1005

but I'm now realising it shouldn't have [ConstantExpected] and also that the method can be public static. I'll fix that in a minute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose "raw" because it was all I could think of to differentiate from the existing one that submits a form. Technically that one takes text as well. This one just posts the text as the payload directly instead of forcing it into a form value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I hadn't seen the extra method. Not sure why it's really needed since there would still be different methods to pass in the different mimeTypes anyway....and for the raw post method I'm exposing the contentType header not to allow encoding the string differently but to allow the script author to set the header as appropriate. This library doesn't need to create a different content object if it's "text/plain" or "application/json". The incoming string is still just a string that's send as the body of the request as StringContent.

payload.Length > ExpectContinueThreshold
).Result;
}

public async Task<string> Get(string url)
{
_client.DefaultRequestHeaders.ConnectionClose = false;
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?.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()
{
Expand Down
Loading