1

I want to upload artifacts to a gocd pipeline using their API which is POST /go/files/:pipeline_name/:pipeline_counter/:stage_name/:stage_counter/:job_name/*path_to_file. I have nodejs application with google oauth2 enabled. I am uploading files and uploading them using POST request in angularjs.

<script>
    angular.module('fupApp', [])
        .directive('ngFiles', ['$parse', function ($parse) {

            function fn_link(scope, element, attrs) {
                var onChange = $parse(attrs.ngFiles);
                element.on('change', function (event) {
                    onChange(scope, { $files: event.target.files });
                });
            };

            return {
                link: fn_link
            }
        } ])
        .controller('fupController', function ($scope, $http) {

            var formdata = new FormData();
            $scope.getTheFiles = function ($files) {
                angular.forEach($files, function (value, key) {
                    formdata.append(key, value);
                });
            };

            // NOW UPLOAD THE FILES.
            $scope.uploadFiles = function () {

                var request = {
                    method: 'POST',
                    url: 'https://<mydomain1.net>:8154/go/files/FirstPipeline/13/defaultStage/1/defaultJob/',
                    data: formdata,
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                  };

                // SEND THE FILES.
                $http(request)
                    .success(function (d) {
                        alert(d);
                    })
                    .error(function () {
                    });
            }
        });
</script>

I am trying to request from mydomain2.net to mydomain1.net(this is gocd). However in browser after selecting a file and clicking on submit, I get this error after I inspect(Ctrl+Shift+I).

Access to XMLHttpRequest at 'https://mydomain1:8154/go/files/FirstPipeline/13/defaultStage/1/defaultJob/' from origin 'http://mydomain2:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Where should the Access-Control-Allow-Origin header be added at GoCD side?

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
Tech Girl
  • 169
  • 2
  • 17
  • 1
    As far as I can tell from looking at the GoCD sources and docs, it has no built-in support for CORS. It seems that instead the recommended way to enable making requests to it from frontend JavaScript code running in a browser is to put a “reverse proxy” in between (similar to what @talhature suggests). See the docs at https://docs.gocd.org/current/installation/configure-reverse-proxy.html – sideshowbarker Oct 15 '19 at 06:17
  • @sideshowbarker, Thank you. I will be considering reverse proxy. Can you help me with where do I need to add the configuration if I want to add the `Access-Control-Allow-Origin` header at gocd server side? Is it after installing nginx where my server is at and then in some file or where? I am completely new to nginx. – Tech Girl Oct 17 '19 at 05:32

1 Answers1

0

A cross-origin error is raised by browsers when one web page that's under a different domain tries to access the resources of a different domain.

If the requester is not a browser, CORS is not applicable. So you can set up a proxy server in between. Check this question's edit section: How to enable CORS in AngularJs

Normally, proxies shouldn't be used in real applications in production, but as far as I understand, what you are trying to achieve is CI/CD integration for your development workflow. So it should be fine.

You can also google how you can set a proxy for AngularJS in NodeJS environment.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
talhature
  • 2,246
  • 1
  • 14
  • 28
  • Thank you for prompt answer! I'll try out reverse proxy : ) – Tech Girl Oct 17 '19 at 04:32
  • Your wellcome, I hope you do not miss the fact that what I reffered as proxy is a reverse proxy here. So don't you think I deserve at least an upvote :) – talhature Oct 18 '19 at 07:03