DX NetOps

 View Only
  • 1.  Post Request resulting in error AFTER Spectrum software upgrade

    Posted Feb 06, 2023 11:34 PM
    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.


  • 2.  RE: Post Request resulting in error AFTER Spectrum software upgrade

    Posted Feb 07, 2023 04:30 AM
    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
    ------------------------------



  • 3.  RE: Post Request resulting in error AFTER Spectrum software upgrade

    Posted Feb 08, 2023 11:08 AM

    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.




  • 4.  RE: Post Request resulting in error AFTER Spectrum software upgrade

    Posted Feb 08, 2023 11:53 AM
    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
    ---


    This is the POST body

    ---
    <?xml version="1.0" encoding="UTF-8"?>
    <rs:model-request throttlesize="300"
    xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
    <rs:landscape id="0x4e200000"/>
    <rs:target-models>
    <rs:models-search>
    <rs:search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
    <filtered-models>
    <and>
    <has-substring>
    <attribute id="0x12adb">
    <value>ALL_INTERFACES</value>
    </attribute>
    </has-substring>
    <has-pcre>
    <attribute id="0xffff0008">
    <value>([a-z])</value>
    </attribute>
    </has-pcre>
    </and>
    </filtered-models>
    </rs:search-criteria>
    </rs:models-search>
    </rs:target-models>

    <rs:requested-attribute id="0x1006e" /> <!-- Model_Name 0x1006e -->
    <!--<rs:requested-attribute id="0x10001" /> Modeltype_Handle 0x1000-->
    <rs:requested-attribute id="0x10000" /> <!-- Modeltype_Name 0x10000 -->
    <rs:requested-attribute id="0x129e7" /> <!-- TopologyModelNameString 0x129e7 -->
    <rs:requested-attribute id="0x13214" /> <!-- CAPC_Item_Id 0x13214 -->
    <!--rs:requested-attribute id="0x10069" /> < Device_Mdl_Handle 0x10069 -->
    <rs:requested-attribute id="0x10069" /> <!-- Device_Mdl_Handle 0x10069 -->

    </rs:model-request>
    ---

    ------------------------------
    Cătălin Fărcășanu
    Senior Consultant
    SolvIT Networks
    ------------------------------



  • 5.  RE: Post Request resulting in error AFTER Spectrum software upgrade

    Posted Feb 14, 2023 01:23 PM

    I found a solution that has provided success but still not sure why this only cropped up after the latest upgrade of Spectrum, however I think it would be good to just include it from the start for any new api calls to prevent this mystery from re-appearing later.

    in the section of code where I am creating the ByteArrayContent I am now adding the Content-Type header;

    ByteArrayContent content = new ByteArrayContent(xmlMessage);
    if (content.Headers.ContentType is null)
    {
        content.Headers.Add("Content-Type", "application/xml");
    }

    Since the upgrade the Request's Accept header is no longer enough, these 3 lines resolved my issue.