locked
Restfull API Post RRS feed

  • Question

  • User221513077 posted

    ASP.NET 5.0 and Visual Studio 2015. 

    I am trying to POST a XML string which is on Windows to RESTFULL API which runs on Linux.

    When I POST the same string to a Windows IIS everything works, but when I post it it to Linux all of the greater then and less then characters in the XML string get reformatted to < and > on Linux side.

    I don't know if it has anything to do with Linux or not.

    Here's my code:

    using (var client = new HttpClient())

    {
    client.BaseAddress = new Uri(ApiUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
    //
    // do REST
    //
    client.Timeout = new TimeSpan(0, 0, 10);
    //
    // POST data
    //
    var response = await client.PostAsXmlAsync(uri, xmlContent);
    }

    What am I doing wrong, or how should I be POSTing the XML data ?

    Thank You

    Peter

    Wednesday, June 17, 2015 4:36 PM

Answers

  • User438962230 posted

    Sorry for the late response, please try this code, create a new HttpRequestMessage object to set the content-type header and the xml content:

    using (var httpClient = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
                    request.Content = new StringContent("xml content here", Encoding.UTF8, "text/xml");
                    var response = await httpClient.SendAsync(request);
                }

    What you also need to do is to contact the service provider for the contract, check if you call the API correctly. It's better to negotiate with the service provider than guessing what kind of request you should send to the service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2015 11:06 PM

All replies

  • User438962230 posted

    Hi czupet,

    It seems that the web server escaped the XML <> tags in case of unsafe string content. Please try to use HtmlEncode to encode the XML content firstly, then send to the server. https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Friday, June 19, 2015 5:55 AM
  • User221513077 posted

    Thanks, but HtmlEncode(String) has no effect, the data shows up the same on the receiving end unless the receiving end is on IIS, if it's on Glassfish or any other server all of the less then and greater then signs in XML show up as &lt;  And &gt;

    and I have no control on the receiving end, because it's customer's end.

    Monday, June 22, 2015 1:08 PM
  • User438962230 posted

    If this doesn't work, I recommend you firstly use Fiddler or some other tools to inspect the request content, make sure you've post the right XML to the server. Try to add content type header to tell the server you're posting a XML file.

    Content-Type: text/xml

    Contact the service provider, make sure you correctly use the service API.

    Tuesday, June 23, 2015 9:33 AM
  • User221513077 posted

    How do I add content type header to the following code?

    using (var client = new HttpClient())

    {
    client.BaseAddress = new Uri(ApiUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
    //
    // do REST 
    //
    client.Timeout = new TimeSpan(0, 0, 10);
    //
    // POST data
    //
    var response = await client.PostAsXmlAsync(uri, xmlContent); 

    }

    Tuesday, June 23, 2015 4:22 PM
  • User438962230 posted

    Sorry for the late response, please try this code, create a new HttpRequestMessage object to set the content-type header and the xml content:

    using (var httpClient = new HttpClient())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
                    request.Content = new StringContent("xml content here", Encoding.UTF8, "text/xml");
                    var response = await httpClient.SendAsync(request);
                }

    What you also need to do is to contact the service provider for the contract, check if you call the API correctly. It's better to negotiate with the service provider than guessing what kind of request you should send to the service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2015 11:06 PM
  • User221513077 posted

    Thank you for you help

    Saturday, June 27, 2015 8:35 PM