Search code examples
javascripthttp-headersgoogle-closure-compilerhttp-status-code-411

Unable to call Closure Compiler web service from Angularjs


Trying to use google closure-compiler with Angular js this is my code/function:

$scope.processForm = function(js_code) {

  var js_code_lenght = js_code.length;
  alert(js_code_lenght);
  console.log($scope.formData);
  $http({
    method: 'POST',
    url: 'http://closure-compiler.appspot.com/compile',
    ///data    : $.param($scope.formData),  // pass in data as strings
    params: {
      'output_info': 'compiled_code',
      'output_format': 'text',
      'compilation_level': 'WHITESPACE_ONLY',
      'js_code': js_code
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
    // set the headers so angular passing info as form data (not request payload)
  })
  .success(function(data) {
    console.log(data);

    if (!data.success) {
      // if not successful, bind errors to error variables
        console.log(data);
    } else {
      // if successful, bind success message to message
      $scope.message = data.message;
    }
  });
};

The problem is in the request header, I receive this error:

POST http://closure-compiler.appspot.com/compile?compilation_level=WHITESPACE_ON…%0D%09%09%09%7D;%0D+%7D();%0D&output_format=text&output_info=compiled_code 411 (Length Required)

Pretty much an error code 411 () Length Required. I wonder is someone out has an idea to solve this issue. I been googling this and I haven't found a solution. thanks.


Solution

  • You must provide your Content-Length header. It's probably something like;

    'Content-Length': $.param($scope.formData).length,