Reputation: 298
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
(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.
Upvotes: 1
Views: 1736
Reputation: 6590
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
Upvotes: 1