Jamie Hartnoll
Jamie Hartnoll

Reputation: 7361

Google Closure Compile REST Api suddenly Throws Error 405 "Method not allowed"

I've been using Closure as part of y application to compress Javascript for a while now, but just started getting an error 405 - Method not allowed

My code as below was working for a couple of years, but has now stopped.

NOTE: this is not called frequently, just when ever any changes are detected in the Javascript files in my application.

I get no further error information from the Closure app than this.

Obviously this code performs a POST operation. If I use the form found here https://closure-compiler.appspot.com/home, it works, but if I either browser to the URL or use my code I get Error 405, it's almost as if my code is trying a GET method... but it's not...

Any ideas?

Public Class Closure

    Property OriginalCode As String
    Property CompiledCode As String
    Property Errors As ArrayList
    Property Warnings As ArrayList
    Property Statistics As Dictionary(Of String, Object)

    Public Sub New(Input As String, Optional CompliationLevel As String = "SIMPLE_OPTIMIZATIONS")

        Dim _HttpWebRequest As HttpWebRequest
        Dim _Result As StringBuilder
        Dim ClosureWebServiceURL As String = "http://closure-compiler.appspot.com/compile?"
        Dim ClosureWebServicePOSTData As String = "output_format=json&output_info=compiled_code" &
                                                                        "&output_info=warnings" &
                                                                        "&output_info=errors" &
                                                                        "&output_info=statistics" &
                                                                        "&compilation_level=" & CompliationLevel & "" &
                                                                        "&warning_level=default" &
                                                                        "&js_code={0}"


        '// Create the POST data
        Dim Data = String.Format(ClosureWebServicePOSTData, HttpUtility.UrlEncode(Input))

        _Result = New StringBuilder
        _HttpWebRequest = DirectCast(WebRequest.Create(ClosureWebServiceURL), HttpWebRequest)
        _HttpWebRequest.Method = "POST"
        _HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
        '//Set the content length to the length of the data. This might need to change if you're using characters that take more than 256 bytes
        _HttpWebRequest.ContentLength = Data.Length
        '//Write the request stream
        Using SW As New StreamWriter(_HttpWebRequest.GetRequestStream())
            SW.Write(Data)
        End Using

        Try

            Dim response As WebResponse = _HttpWebRequest.GetResponse()

            Using responseStream As Stream = response.GetResponseStream
                Dim encoding As Encoding = System.Text.Encoding.GetEncoding("utf-8")
                Using readStream As New StreamReader(responseStream, encoding)
                    Dim read(256) As Char
                    Dim count As Integer = readStream.Read(read, 0, 256)
                    While count > 0
                        Dim str As New String(read, 0, count)
                        _Result.Append(str)
                        count = readStream.Read(read, 0, 256)
                    End While
                End Using
            End Using

            Dim js As New JavaScriptSerializer
            js.MaxJsonLength = Int32.MaxValue

            Dim d As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(_Result.ToString())
            Me.CompiledCode = d.NullKey("compiledCode")
            Me.Warnings = TryCast(d.NullKey("warnings"), ArrayList)
            Me.Errors = TryCast(d.NullKey("errors"), ArrayList)
            Me.Statistics = TryCast(d.NullKey("statistics"), Dictionary(Of String, Object))

        Catch ex As Exception
            Me.CompiledCode = ""
            If Me.Errors Is Nothing Then
                Dim er As New List(Of String)
                er.Add(ex.ToString())
                Me.Errors = New ArrayList(er)
            Else
                Me.Errors.Add(ex.ToString())
            End If
        End Try

        Me.OriginalCode = Input

    End Sub

End Class

Upvotes: 3

Views: 277

Answers (1)

Mr.X
Mr.X

Reputation: 76

Closure REST api is redirecting to https, you may want to try to POST to "https://closure-compiler.appspot.com/compile" directly in order to avoid redirection.

Upvotes: 5

Related Questions