1

I'm attempting to manually send some XML content to an ASP MVC Web Api server that I've created. The Controller.Put() method looks like this:

public Game Put(int id, [FromBody] HttpAction[] actions)
{
     Debug.WriteLine(actions[0].TargetId + ", " + actions[0].Type + ", " +   actions[0].ContentType + ", " + actions[0].Contents);
     Game game = this.provider.Update(id, actions);
     return game; 
}

The null reference occurs immediately when checking the parameters on the action object. This method receives an object Id and an array of type HttpAction which looks like this:

[DataContract]
public class HttpAction
{
    [DataMember]
    public int TargetId { get; set; }
    [DataMember]
    public HttpActionType Type { get; set; }
    [DataMember]
    public HttpActionContentType ContentType { get; set; }
    [DataMember]
    public string Contents { get; set; }
}

I've setup my PUT request like this:

Header

  • Content-Type: application/xml
  • Accept: */*
  • Accept-Encoding: gzip, deflate, sdch
  • Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

(most of the above is generated by Advanced Rest Client which I am using to send the request)

Body

<?xml version="1.0" encoding="utf-8"?>
<HttpActions>
   <HttpAction>
     <TargetId>0</TargetId>
     <Type>Add</Type>
     <ContentType>Player</ContentType>
     <Contents>UnityPlayer</Contents>
   </HttpAction>
</HttpActions>

Another attempt at the body:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfHttpAction xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HttpAction>
  <TargetId xmlns="http://schemas.datacontract.org/2004/07/GOHCGLibrary.Actions">0</TargetId>
  <Type xmlns="http://schemas.datacontract.org/2004/07/GOHCGLibrary.Actions">Add</Type>
  <ContentType xmlns="http://schemas.datacontract.org/2004/07/GOHCGLibrary.Actions">Player</ContentType>
  <Contents xmlns="http://schemas.datacontract.org/2004/07/GOHCGLibrary.Actions">UnityPlayer</Contents>
</HttpAction>
</ArrayOfHttpAction>

Whenever I send this request I find that the body of the request is null in the controller. I've managed to test it while using JSON body and it works fine, I also have unit tests on my controller that pass in HttpAction arrays to check all the background code works fine which it does.

What am I doing wrong when it comes to constructing the XML for the request? I've read that I need to include xmlns and xmlns:i but I'm not sure what these are for or what to set them to. I've tried various options with no success.

1 Answer 1

1
+50

Change you method to this

public Game Put(HttpAction[] actions)
{
}

For Single item you should try this request body.

<HttpAction xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebApiSample.Models">
  <ContentType>sample string 3</ContentType>
  <Contents>sample string 4</Contents>
  <TargetId>1</TargetId>
  <Type>sample string 2</Type>
</HttpAction>

Try this content type

Content-Type: application/xml

For List of HttpAction you should try following type of request body

<ArrayOfHttpAction xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebApiSample.Models">
 <HttpAction>
    <ContentType>sample string 3</ContentType>
    <Contents>sample string 4</Contents>
    <TargetId>1</TargetId>
    <Type>sample string 2</Type>
 </HttpAction>
 <HttpAction>
    <ContentType>sample string 3</ContentType>
    <Contents>sample string 4</Contents>
    <TargetId>1</TargetId>
    <Type>sample string 2</Type>
 </HttpAction>
</ArrayOfHttpAction>

Note: Don't use <?xml version="1.0" encoding="utf-8"?> in you request body

Sign up to request clarification or add additional context in comments.

4 Comments

The key part is the data contract schema. It should have the namespace of where your HttpAction class is defined. xmlns="http : // schemas.datacontract.org/2004/07/<Enter your namespace here>". I do not know how to get the comment to not show this as a link so remove the extra spaces.
@Brian I tried same at my end with same data contract and it working fine
If the location of the class is not referenced correctly in the data contract schema then the code does not know what object to serialize the data into. In my case if I cut and paste your example I still get a null because my class was created in the "SampleMVC.Controllers" namespace. @Ajay
Hi guys, I haven't had a chance to test this yet, I will do so tonight and let you know the results on my setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.