cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1685
Views
5
Helpful
1
Replies

How to POST with Rest API to an ACS server

fwiest
Level 1
Level 1

I have been trying to get the Rest API to work with the ACS server and am struggling with the POST commands. I am using C# but to simplify things I am testing with the Rest Client plugin for Firefox. My URL is:

https://ACSServer01/Rest/Identity/User

And my body is as follows:

<ns2:user>
<description>xxx</description>
<identityGroupName>All Groups:Network Administrator</identityGroupName>
<name>testfred</name>
<changePassword>false</changePassword>
<dateExceedsEnabled>false</dateExceedsEnabled>
<enablePassword>thisisalso!apass</enablePassword>
<enabled>true</enabled>
<password>thisisa!pass</password>
<passwordNeverExpires>false</passwordNeverExpires>
<passwordType>LDAP EMEA</passwordType>
</ns2:user>

But I get a parsing error when I execute it. Can someone tell me what I am missing?

1 Reply 1

fwiest
Level 1
Level 1

I solved the REST client issue by adding a content-type=application/xml in the header and the following line at the top of the body.

<ns2:device xmlns:ns2="networkdevice.rest.mgmt.acs.nm.cisco.com">

So now I am back to the C#. I get an error 400 code when running the following. I can do a GET without an error but it doesn't like my POST. Anyone with experience with this?

 

Uri address = new Uri("https://" + server + "/Rest/Identity/User");
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(_username, _password);
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
request.Method = "POST";
request.MediaType = "xml";
request.ContentType = "application/xml";
request.Accept = "application/xml";

StringBuilder data = new StringBuilder();
data.Append("<ns2:user ");
data.Append("name='" + "testUser" + "' ");
data.Append("description='" + "test desc" + "' ");
data.Append("identityGroupName='" + "All Groups:Network Administrator" + "' ");
data.Append("changePassword='" + "false" + "' ");
data.Append("enablePassword='" + "thisisalso!apass" + "' ");
data.Append("password='" + "thisisa!pass" + "' ");
data.Append("passwordNeverExpires='" + "false" + "' ");
data.Append("passwordType='" + "Internal Users" + "' ");
data.Append("enabled='" + "true" + "' ");
data.Append("/ns2:user>");

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = byteData.Length;

using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
}
response = request.GetResponse() as HttpWebResponse;