Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Just been stuck here for long time now. Thought someone might be able to put me in right direction. I have created a ASP.NET Web API 2 application. There is one function which will return all ships. Now I want to receive an xml inside my controller function. How can I achieve that. Below is my HttpClient calling code which is giving me Bad Request error. Can someone guide me in right direction to how to pass xml to Web api function.

Routing

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

Controller Action

public IHttpActionResult GetSummaryFunction(string id)
        {
            return Ok(id);
        }

Client Code

HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:53633/");

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/xml"));

            string testXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SummaryRQ  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Test.WebAPI.Controllers\" Version=\"1.00\" Target=\"Test\"><POS><Source Login=\"test\" Password=\"test\"></Source></POS></SummaryRQ>";

            var url = "api/summaryfunction/" + testXml;

            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
            }
            else
            {

            }
share|improve this question
add comment

1 Answer

At minimum you are going to need to pass the testXml string to the Uri.EscapeDataString function.

You may also run into some issues if your Xml is too big. How big is too big depends a lot on the infrastructure that exists between your client and server.

share|improve this answer
    
Thanks for the reply but Uri.EscapeDataString didn't work. Infact I tried very simple xml with <test>test</test> and that failed with Bad Request error as well. Not sure how to pass xml here. –  Leo Apr 9 at 17:00
    
@Leo You need to show what your Controller action method signature is and also your routing. –  Darrel Miller Apr 9 at 17:15
    
Question has been edited to include that. Thanks –  Leo Apr 9 at 17:27
    
@Leo Is you controller called SummaryFunctionController ? And do you really want that entire XML as the value of the id parameter? –  Darrel Miller Apr 9 at 17:39
    
@LEo Normally you would pass the XML in the body of a POST or a PUT. In theory there is nothing wrong with putting it as a parameter of the URI though. –  Darrel Miller Apr 9 at 17:40
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.