Post Json to Rest which listen inside a Windows Service RRS feed

  • Question

  • I have a method inside a Windows Service, when I send a GET all works fine.

    I am trying to send post :

    on windows service :

    [WebInvoke(Method = "POST", RequestFormat= WebMessageFormat.Json, UriTemplate = "SendFlash")]
            void SendFlash(string data);

    from another application which send the json :


    HttpClient httpClient = new HttpClient() { BaseAddress = new Uri(ConfigurationManager.AppSettings["SendURL"].ToString()) }; _httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //httpMessages is a List<T> of classes string data = JsonConvert.SerializeObject(httpMessages); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/SendFlash"); request.Content = new StringContent(data,Encoding.UTF8,"application/json"); using (HttpResponseMessage result = await _client.SendAsync( request)) { if (result.StatusCode == HttpStatusCode.OK) { } else//result.StatusCode { } }

    i can't find what i am mising to make it work.

    any inputs what can be wrong?

    **also I see that in the header there is 3 times application/json (and I wanted to add charset=utf-8)

    application json header

    Wednesday, March 4, 2020 11:47 PM

All replies

  •  did some progress

    now i get the method called :

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "SendFlash")]
            void SendFlashSMS( );

    but when I try to get the body add stream to method:

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "SendFlash")]
            void SendFlashSMS( Stream pStream);

    the method not called.

    when i try the first approche, i read the body using :

    string body=OperationContext.Current.RequestContext.RequestMessage.ToString();

    i see that my json is converted into :

    <root type="array">
      <item type="object">
        <DirectAccountID type="number">44</DirectAccountID>
        <Text type="string">xxxxxxx</Text>
      <item type="object">
        <DirectAccountID type="number">123456</DirectAccountID>
        <Text type="string">yyyyyy</Text>
    i assume something is missing in the WebInvoke declaration which convert it from json to XML

    Wednesday, March 4, 2020 11:47 PM
  • Hi,

    How do you call the Post method?  The below method declaration work properly on my side.

            [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            Task UploadStream(Stream stream);

    I call this kind of method by using Postman.

    Feel free to let me know if there is anything I can help with.

    Friday, March 6, 2020 7:57 AM