Skip to main content
Inspiring
April 21, 2023
Answered

cfhttp call within REST service returning 301 Moved Permanently

  • April 21, 2023
  • 2 replies
  • 2740 views

In building out a new server for ColdFusion 2021, I migrated code running on CF2016 which is returning a strange error. One of my GET REST services which calls another using cfhttp is returning the following:

 

{
Message: "JSON parsing failure at character 1:'<' in <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://myservername.ddns.net/rest/services/grantPermission">here</a>.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at myservername.ddns.net Port 80</address>
</body></html>"
}

 

What makes this even stranger is the fact that there is no redirect in either service.

See the below snippet of error from the exception log:

"Error","ajp-nio-127.0.0.1-8020-exec-2","04/21/23","18:57:09","","HTTP 500 Internal Server Error"
coldfusion.rest.method.dispatch.CFRestException: HTTP 500 Internal Server Error

 

Flowed by:

Caused by: coldfusion.xml.rpc.CFCInvocationException: [coldfusion.runtime.JSONUtils$JSONParseException : JSON parsing failure at character 1:'<' in <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://myservername.ddns.net/rest/services/grantPermission">here</a>.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at myservername.ddns.net Port 80</address>
</body></html>. ]

 

Has anyone encountered this issue before?

    Correct answer KARMA28699455uxq6

    The description in your first message says the issue is about "One of my GET REST services". But the request you showed later is:

    <cfhttp url="#webservice#" method="post" .../>

    which is a POST instead of a GET.

    Confusing.

     


    I found the issue shortly after responding to your previous email. localservice referenced in the call was using a CGI variable in my dev environment which was apparently adding something to the URL string which was not shown in the error message. I changed it to call the full server name instead which resolved the issue.

     

    Many thank to you and @Charlie Arehart for your patience and effort in assisting me with troubleshooting this issue.

     

    2 replies

    EddieLotter
    Inspiring
    April 21, 2023

    Firstly, it appears that you are assuming you are getting a JSON response, which of course fails when you get an HTTP response. You should first verify that the response you got is indeed JSON before trying to treat it as such.

     

    Next, have you used PostMan or cUrl or a similar utility to test the endpoint to verify that the response is what you expect?

    Inspiring
    April 21, 2023

    Hi @EddieLotter,

    I have verified that the response is indeed JSON. See the response below:

    {
        "MESSAGE": "A Certificate will be issued.",
        "DOCUMENT": "cert",
        "TMP_DOCUMENT": "cert"
    }

     This is a snippet of the function within the component of what is being called:

    <cffunction access="remote" name="grantPermission" output="false" httpmethod="POST" returnFormat="plain" returntype="Any"  produces="application/json">

     

    BKBK
    Community Expert
    Community Expert
    April 22, 2023

    That message is returned when I call a service that is calling the grantPermission service via cfhttp


    Hi @KARMA28699455uxq6 ,

    There is no mystery about the error you're getting. It results from the following sequence of events:

    1.  The REST request posts to the wrong URL;
    2.  The web server's status-code is 301 Moved Permanently. The corresponding response is a default HTML 301 response page.
    3.  As the HTML consists of tags, the following code 
      <cfset jsonData = DeserializeJSON(grantPermissionRes.fileContent)>​

      generates the error 

      "JSON parsing failure at character 1:'<' in <!DOCTYPE HTML PUBLIC "
      So, long story short, the REST request is posting to the wrong URL
    Charlie Arehart
    Community Expert
    Community Expert
    April 21, 2023

    To answer your question, I'll say no (have not seen this before). But I suspect this is not about cf2021 but instead either a) your new server or b) the Java version underlying your two versions.

     

    Can you clarify what your cf2021 and cf2016 Java versions are, and the cf update level of each? Both are shown on the cf admin settings summary or system info pages.

     

    Also, if you want to prove/disprove my theory, you could install cf2016 on the new server--yes, even just for this one test. It would take just minutes. And no need to setup the web server connector. Just drop a test page into the cfusion/wwwroot of cf2016, and call it with the same port used for the cf admin.

     

    And note that there were important changes to the jvm in Apr 2021, which affect calling out to https urls, if the server being called does not support tls 1.2. If your cf2016 had been running earlier than Java 1.8.0_291 or 11.0.11, and your cf2021 is 11.0.11 or later, that could underlie your issue. I have a blog post with more detail:

    https://www.carehart.org/blog/2021/4/26/new_java_updates_for_Java_8_and_11_as_of_Apr_2021

     

    As for the redirect, you may want modify your cfhttp to not follow redirects, to what it then responds with (dumping the cfhttp result variable to see what it shows).

     

    Hope something there is helpful. 

    /Charlie (troubleshooter, carehart. org)
    Inspiring
    April 21, 2023

    Thanks for your speedy response. I suspected it could have been an issue with the Java version so I upgraded from jre 11.0.11 to jdk 11.0.18. Unfortunately, there was no change.

     

    The service being called is actually on the server and it has TLSv1.2 and 1.3 enabled.

     

    I tried disabling the redirect as you suggested, but I got the same response.

     

     

    Charlie Arehart
    Community Expert
    Community Expert
    April 21, 2023

    Ok, but since you say your java was 11.0.11, I did not suggest you update that. I'd said "If your cf2016 had been running earlier than Java 1.8.0_291 or 11.0.11, and your cf2021 is 11.0.11 or later, that could underlie your issue."

     

    Also, I'd asked what was the Java version and cf update level for both. I sense you may feel these are not pertinent, but I will persist as it could be relevant.

     

    Finally, I still would argue you look at the cfhttp return variable. Like Eddie is saying, you may see something that surprises you. 

    /Charlie (troubleshooter, carehart. org)