I banged my head on the wall trying to script some activities with curl and noticed that from I don't know what reason, if the header attributes set for the call are not in a certain order, then SPECTRUM is rejecting the call, with the 415 message. This, even if you set the filetype by call to XML and AFAIK, the Header attributes order should not matter. I could was not able to find a valid reason for why this would happen, but I stopped investigating altogether when I got a version that worked. It was frustrating, indeed.
I assume you're have your own code written so you can try to change the order of the header parameters to see if it works.
This would be the one that works for me. I noticed the same problem when I upgraded my installation to 21.2.1.
curl -k -s -H "Authorization: Basic $ochash" -H "Content-Type: application/xml" -X POST "$ocprotocol://$ocserver:$ocport/spectrum/restful/models" -d @$lib/GetInterfaces.xml
xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
Original Message:
Sent: Feb 08, 2023 11:07 AM
From: Schott Beckham
Subject: Post Request resulting in error AFTER Spectrum software upgrade
Thanks Catalin for your response, I've changed the EOL's to LF in the message body and unfortunately still getting the error. Was able to get the Systems team to find the error in the spectrum log and the following error was in it for the test.
No message body reader has been found for class com.ca.spectrum.restful.schema.request.EventRequest, ContentType: application/octet-stream
triggering the 415 (Unsupported Media Type) error
The odd part is the software generating and sending the messages via the REST api hasn't changed in a very long time; this only started after the Spectrum upgrade. The default accept header is set to "application/xml". I am baffled; is there a setting on the Spectrum server side to allow xml files to be passed in that might have not carried over from the previous version. Unfortunately the only instance we have of the previous version is in Production which is is off limits for trying things.
Thanks in advance for any guidance.
Original Message:
Sent: Feb 07, 2023 04:30 AM
From: Catalin Farcasanu
Subject: Post Request resulting in error AFTER Spectrum software upgrade
I would try the EOL from the body changed to LF. I think that that could be the problem.
------------------------------
Cătălin Fărcășanu
Senior Consultant
SolvIT Networks
Original Message:
Sent: Feb 06, 2023 11:33 PM
From: Schott Beckham
Subject: Post Request resulting in error AFTER Spectrum software upgrade
Our systems team recently upgraded Spectrum to version 21.2.6; and as far as we can tell ever since the upgrade we are getting "Unsupported Media Type" errors when posting event request via the API. It was working prior to the upgrade and nothing has changed in the code for posting these requests for some time now. Has anyone experieinced this with the Spectrum API after an upgrade? Is there a option that needs to be turned on in the new spectrum. I am only familiar with the API aspect of Spectrum.
Details from the action of posting a request to the OCS. Sensitive information has been swapped with non-sensitive data.
This is the format of the url we're sending the message to
http://OurSpectrumEndpoint:8080/spectrum/restful/events/
This is the message body were sending
<?xml version="1.0" encoding="utf-8"?>
<rs:event-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd" throttlesize="10" xmlns:rs="http://www.ca.com/spectrum/restful/schema/request">
<rs:event>
<rs:target-models>
<rs:model mh="0x200326" />
</rs:target-models>
<rs:event-type id="0x03b10107" />
<rs:varbind id="1">varbind 1 value</rs:varbind>
<rs:varbind id="101">varbind 101 value</rs:varbind>
<rs:varbind id="102">varbind 102 value</rs:varbind>
<rs:varbind id="103">varbind 103 value</rs:varbind>
<rs:varbind id="104">varbind 104 value</rs:varbind>
<rs:varbind id="105">varbind 105 value</rs:varbind>
<rs:varbind id="206">varbind 206 value</rs:varbind>
</rs:event>
</rs:event-request>
From looking at the example event request in the API documentation I for the life of me cannot see any problems; why I'm on here asking.
The configuration of the authentication for the HttpClient is done like this
var byteArray = Encoding.ASCII.GetBytes(_spectrumAuthentication);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
The setting of the HttpClient default request header is done like this
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/XML"));
This is the PostRequest message (minus the log entries)
private async void PostRequest(byte[] xmlMessage, Uri uri)
{
try
{
ByteArrayContent content = new ByteArrayContent(xmlMessage);
if ((client.DefaultRequestHeaders.Authorization == null) || (client.DefaultRequestHeaders.Accept.Count < 1))
{
configureHttpClient();
}
else
{
_logger.LogInformation("Authentication header and Content-Type header present in message headers");
}
HttpResponseMessage response = await client.PostAsync(uri, content);
_logger.LogInformation("Response from Spectrum: {0} - {1}", (int)response.StatusCode, response.StatusCode.ToString());
//If unable to authorize, check for redirect. If redirected, verify validty of new url. If valid resend request.
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.LogInformation("Checking spectrum response for a redirect uri: {0}", response.RequestMessage.RequestUri.ToString());
var finalRequestUri = response.RequestMessage.RequestUri;
if (finalRequestUri != uri)
{
//TODO: Add a validation for url
response = client.GetAsync(finalRequestUri).Result;
_logger.LogInformation("Response from Spectrum redirect: {0} - {1}", response.StatusCode.ToString(), response.ReasonPhrase);
}
else
{
_logger.LogInformation("Redirect uri is the same as the initial uri");
}
}
if ((int)response.StatusCode != 200)
{
_logger.LogInformation("Content header size: {0}", (content.Headers.ContentLength / "application/XML".Length));
_logger.LogInformation("Total header count: {0}", content.Headers.ToString());
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message + "\n" + ex.StackTrace);
}
}
This is the serialization before sending it off to the PostRequest method above
private byte[] SeralizeMessageContent(EventRequest request)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("rs", "http://www.ca.com/spectrum/restful/schema/request");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
Encoding = Encoding.UTF8
};
using (MemoryStream memoryStream = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(memoryStream, xmlWriterSettings))
{
XmlSerializer xmlSerializer = new XmlSerializer(request.GetType());
xmlSerializer.Serialize(writer, request, ns);
// Package the complete message in a byte array.
byte[] eventMessage = memoryStream.ToArray();
//Debug Write Xml Message to Console
Console.WriteLine(Encoding.UTF8.GetString(eventMessage));
return eventMessage;
}
}
}
Again this all worked fine up until the Spectrum upgrade in late November, but now that the integration has been turned back on we are getting an "Unsupported Media Type" response.
2023-02-06 19:35:22.9660 [INFO] OurSpectrumApplicationName.SpectrumReportingService+<PostRequest>d__72.MoveNext Response from Spectrum: 415 - UnsupportedMediaType
Any insight would be greatly appreciated.