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?
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.