none
Unable to Create a Simple POST Method in Universal App RRS feed

  • Question

  • Hello there

    I am trying to work out how to create a simple POST to a web service.  I am writing both the web service and the front end Universal app.

    I am able to get this to work up to a point using something similar to the following:

     

    [OperationContract]
    [WebInvoke(UriTemplate = "/sPostItemsSyncDetails?sHeader={sHeader}&sBody={sBody}", Method ="POST")]
    string sPostItemsSyncDetails(string sHeader, string sBody);

     

    HttpClient httpClient = new HttpClient();
    HttpResponseMessage response;
    string responseText;
    string sBody = “MyBody”;
    StringContent sbContent = new StringContent(sBody);

    response = await httpClient.PostAsync(“http://localhost/YogieAppService/YogieAppService.svc/sPostItemsSyncDetails?sHeader=MyHeader&sBody=” + sBody, sbContent);

    responseText = await response.Content.ReadAsStringAsync();

     

    Note that I am setting “sbContent” as the 2<sup>nd</sup> parameter to the call to httpClient.PostAsync(), as this parameter is required, however this isn’t used server side, not do I know how to access it server side.

    This works for smaller values of sBody, however it soon runs into the following error:

    The request filtering module is configured to deny a request where the query string is too long.

    It seems like I could probably resolve this by allowing a larger querystring, however I really would like to do it “propery”, but have been unable to find a suitable simple example of what I should be doing, i.e. the best way to develop and call a simple POST method passing a smaller header and a larger body, both as strings.  As I say, I am passing some content as the 2<sup>nd</sup> parameter to the call to PostAsync(), but have found no suitable example of how this is picked up by the server.

    Any help greatly appreciated.

    Saturday, January 13, 2018 9:41 PM

Answers

  • Hi Geoff_Olding,

    >>Note that I am setting “sbContent” as the 2<sup>nd</sup> parameter to the call to httpClient.PostAsync(), as this parameter is required,

    Could you share us the reason you pass sBody as body and header?

    There is size limit on query string, it is not recommended to pass sBody through header. And as my understanding, you already could get sBody from body, there is no need to pass it again through header.

    For one option, I suggest you try code below:

    WCF Service

            [OperationContract]
            //[WebInvoke(UriTemplate = "/sPostItemsSyncDetails?sHeader={sHeader}&sBody={sBody}", Method = "POST")]
            [WebInvoke(UriTemplate = "/sPostItemsSyncDetails?sHeader={sHeader}", Method = "POST")]
            string sPostItemsSyncDetails(string sHeader);
    -----------------------------------------------
            public string sPostItemsSyncDetails(string sHeader)
            {
                //get body from request message
                string body = Encoding.UTF8.GetString(OperationContext.Current.RequestContext.RequestMessage.GetBody<byte[]>());
    
                return sHeader;
            }
    

    Client Request

    HttpClient httpClient = new HttpClient();
    HttpResponseMessage response;
    string responseText;
    string MyHeader = "MyHeaderValue";
    string sBody = "MyBodyValue";
    StringContent sbContent = new StringContent(sBody);
    
    response = await httpClient.PostAsync("http://localhost/YogieAppService/YogieAppService.svc/sPostItemsSyncDetails?sHeader=" + MyHeader, sbContent);
    
    responseText = await response.Content.ReadAsStringAsync();

    Best Regards,

    Tao Zhou 


    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.

    • Marked as answer by Geoff_Olding Friday, January 26, 2018 10:54 PM
    Monday, January 15, 2018 2:03 AM

All replies

  • Hi Geoff_Olding,

    >>Note that I am setting “sbContent” as the 2<sup>nd</sup> parameter to the call to httpClient.PostAsync(), as this parameter is required,

    Could you share us the reason you pass sBody as body and header?

    There is size limit on query string, it is not recommended to pass sBody through header. And as my understanding, you already could get sBody from body, there is no need to pass it again through header.

    For one option, I suggest you try code below:

    WCF Service

            [OperationContract]
            //[WebInvoke(UriTemplate = "/sPostItemsSyncDetails?sHeader={sHeader}&sBody={sBody}", Method = "POST")]
            [WebInvoke(UriTemplate = "/sPostItemsSyncDetails?sHeader={sHeader}", Method = "POST")]
            string sPostItemsSyncDetails(string sHeader);
    -----------------------------------------------
            public string sPostItemsSyncDetails(string sHeader)
            {
                //get body from request message
                string body = Encoding.UTF8.GetString(OperationContext.Current.RequestContext.RequestMessage.GetBody<byte[]>());
    
                return sHeader;
            }
    

    Client Request

    HttpClient httpClient = new HttpClient();
    HttpResponseMessage response;
    string responseText;
    string MyHeader = "MyHeaderValue";
    string sBody = "MyBodyValue";
    StringContent sbContent = new StringContent(sBody);
    
    response = await httpClient.PostAsync("http://localhost/YogieAppService/YogieAppService.svc/sPostItemsSyncDetails?sHeader=" + MyHeader, sbContent);
    
    responseText = await response.Content.ReadAsStringAsync();

    Best Regards,

    Tao Zhou 


    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.

    • Marked as answer by Geoff_Olding Friday, January 26, 2018 10:54 PM
    Monday, January 15, 2018 2:03 AM
  • Hello there

    This has worked - thanks for your help.

    With respect to your questions, I realised that my code was wrong - it was just something I put together to keep me going whilst I unsuccessfully looked for some basic documentation.

    Regards

    Geoff Olding

    Friday, January 26, 2018 10:54 PM