Jump to content

Send json payload with IHttpClient


darkside40

Recommended Posts

darkside40

I think i am a bit too dumb.

Could somebody explain to me how to send a simple json Payload with the IHttpClient?

I know that it only accepts dictionary, which is my problem. The server should simply get a payload like this

{"msgtype":"m.text","body":"hello"}

To construct the message is quite easy:

var message = new Message {body = "hello", msgtype = "m.text" };

var parameters = new Dictionary<string, string> { };
parameters.Add("",_jsonSerializer.SerializeToString(message));

An then sending the parameter with the Ihttpclient, what the Server gets is of course (because of the Empty String in the dictionary):

={"msgtype":"m.text","body":"hello"}

Does anybody have a simple solution how to construct or send such a simple json payload with emby?

Link to comment
Share on other sites

Hi, on HttpRequestOptions, set RequestContent to the content, and then RequestContentType to application/json

Link to comment
Share on other sites

darkside40

Okay after figuring out how to convert my serialized string to char (which RequestContent expects) i was able to send the first Message.

Sometime i ask myself why thing must be so compicated in C#.

Nevertheless thank you @Luke now i have to solve that i get an error when the MEssage was transmitted, Maybe something with the return code. Any Ideas?

Link to comment
Share on other sites

darkside40

grafik.png.63f9fd10011995300670ffce812d1adc.png

2022-02-08 18:11:36.075 Info HttpClient: POST http://192.168.2.107:8181
2022-02-08 18:11:36.090 Error Server: Error processing request
	*** Error Report ***
	Version: 4.6.7.0
	Command line: C:\Users\b\Downloads\embyserver-win-x64-4.6.7.0\system\EmbyServer.dll
	Operating system: Microsoft Windows 10.0.19042
	Framework: .NET Core 3.1.21
	OS/Process: x64/x64
	Runtime: C:/Users/b/Downloads/embyserver-win-x64-4.6.7.0/system/System.Private.CoreLib.dll
	Processor count: 8
	Data path: C:\Users\b\Downloads\embyserver-win-x64-4.6.7.0\programdata
	Application path: C:\Users\bsengotta\Downloads\embyserver-win-x64-4.6.7.0\system
	System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object.
	   at Emby.Plugin.MessageNotification.Api.ServerApiEndpoints.PostAsync(TestNotification request)
	Source: Emby.Plugin.MessageNotification
	TargetSite: Void MoveNext()

Like i said, the Notification is send without issues, but i only have the bare minimum in my code to do so. So maybe it because i dont check the return code.

        public async Task PostAsync(TestNotification request)
        {
            var options = GetOptions(request.UserID);

            var parameters = new Dictionary<string, string> { };
            parameters.Add("msgtype", "m.text");
            parameters.Add("body", "hello");
            string json = _jsonSerializer.SerializeToString(parameters);

            char[] content = json.ToCharArray();
            var httpRequestOptions = new HttpRequestOptions
            {
            Url = "http://192.168.2.107:8181",
             CancellationToken = CancellationToken.None
            };
            httpRequestOptions.RequestContentType = "application/json";
            httpRequestOptions.RequestContent = content;


            using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
            {

            }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...