none
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 :

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

    from another application which send the json :

    //SendURL-->http://localhost/Service

    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


    • Edited by want 2 Learn Wednesday, March 4, 2020 2:20 PM added info
    Wednesday, March 4, 2020 2:16 PM

Answers

  • I solved it

    the problem that

    it should be

    _httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
    
    and not
    _httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    Wednesday, March 11, 2020 11:14 AM
  • Hi,

    Thank you for posting here.

    You can try the following code to convert xml to json.

     string xml = @" <root type='array'>
                                  <item type = 'object'>
                                     <DirectAccountID type = 'number' > 44 </DirectAccountID >
                                     <Text type = 'string' > xxxxxxx </Text >
                                  </item >
                                  <item type = 'object'>
                                     <DirectAccountID type = 'number' > 123456 </DirectAccountID >
                                     <Text type = 'string' > yyyyyy </Text >
                                  </item>
                                </root>";
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
    
                string json = JsonConvert.SerializeXmlNode(doc);
                Console.WriteLine(json);

    Result: 

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 5, 2020 5:42 AM

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>
      <item type="object">
        <DirectAccountID type="number">123456</DirectAccountID>
        <Text type="string">yyyyyy</Text>
      </item>
    </root>
    i assume something is missing in the WebInvoke declaration which convert it from json to XML


    • Edited by want 2 Learn Wednesday, March 4, 2020 3:24 PM fixed
    Wednesday, March 4, 2020 3:23 PM
  • If you are looking at the result with  a Web browser and the result is Json, then it's going to be displayed as XML even though it's Json. 

    And why are you using WCF and not ASP.NET WebAPI with WebAPI data exchange default is Json?

    Wednesday, March 4, 2020 8:58 PM
  • it's not only that I see xml, I try to deserialize to json and it fails.

    at the moment i can't switch too  ASP.NET WebAPI

    Wednesday, March 4, 2020 9:58 PM
  • If your dealing with WCF, it has a forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

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

    Thank you for posting here.

    You can try the following code to convert xml to json.

     string xml = @" <root type='array'>
                                  <item type = 'object'>
                                     <DirectAccountID type = 'number' > 44 </DirectAccountID >
                                     <Text type = 'string' > xxxxxxx </Text >
                                  </item >
                                  <item type = 'object'>
                                     <DirectAccountID type = 'number' > 123456 </DirectAccountID >
                                     <Text type = 'string' > yyyyyy </Text >
                                  </item>
                                </root>";
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
    
                string json = JsonConvert.SerializeXmlNode(doc);
                Console.WriteLine(json);

    Result: 

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 5, 2020 5:42 AM
  • hello friend you define HttpClient httpClient = new HttpClient()
    _httpClient .DefaultRequestHeaders // why underscore used here with httpClient
    Thursday, March 5, 2020 7:29 AM
  • Hi,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 11, 2020 10:02 AM
  • I solved it

    the problem that

    it should be

    _httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
    
    and not
    _httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    Wednesday, March 11, 2020 11:14 AM
  • Hi,

    It seems that your problem has been solved. Please spend some time marking one or more correct responses as answers (including your own), this will greatly help users with similar problems.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 16, 2020 5:40 AM