AnsweredAssumed Answered

How to attach XML Document to RestSharp call

Question asked by Markus Dauberschmidt on Feb 8, 2017

Dear Qualys Community, I would like to utilize the Cloud Agent API to programmatically turn on/off the VM and PC modules on certain hosts. I intend to use RestSharp in C#.

 

I've done these things before, but the former approaches all generate a "UnsupportedMediaType" with Qualys.

 

This is my approach:

 

XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes" ) );
XElement serviceRequest = new XElement("ServiceRequest");
XElement filters = new XElement("filters");
XElement Criteria = new XElement("Criteria");
XAttribute field = new XAttribute("field","tagName");
XAttribute _operator = new XAttribute("operator","EQUALS");
Criteria.Add(field);
Criteria.Add(_operator);
Criteria.SetValue("Cloud Agent");
doc.Add(serviceRequest);
serviceRequest.Add(filters);
filters.Add(Criteria);

var request = new RestRequest("/qps/rest/2.0/deactivate/am/asset?module=AGENT_VM", Method.POST);
// Add header, reqiured for v2 API.
request.AddHeader("X-Requested-With", "RestSharp");
request.AddHeader("Content-Type", "application/xml");

request.RequestFormat = DataFormat.Xml;

StringWriter sw = new StringWriter();
doc.Save(sw);
byte[] dataBuffer = Encoding.UTF8.GetBytes(sw.ToString());

request.AddFile("file", dataBuffer, "text/xml" );
response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Success disable AGENT_VM");
}

 

client.Execute gives me back an:

UnsupportedMediaType, Error E_INFRA_1007 in JSON format.

 

I've already experimented with AddFile(), AddBody() but in vain.

 

Any best practices to programmatically generate and add the XML document to the request?

Outcomes