I have a curl command which uploads a .zip file to the nexus repository. I want to call this command in Angular. I am wondering how it has to be done. Can you please help me out. command: curl -v -u username:password --upload-file test.zip http://use08nexus01p:8081/nexus/content/repositories//
1 Answer
curl's --upload-file parameter uses a PUT request to xfer the data to your server. The -u parameter uses basic authentication. There are a lot of ways you could send a similar request using Angular. You will likely have a service that does this work for you. Below is one possible way to accomplish this. Note that the service function below returns an observable that will need to be subscribed to in a component. Perhaps something like the following:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpEvent, HttpParams, HttpRequest, HttpHeaders } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class AuthService {
constructor(private http: HttpClient) {}
public uploadFile(file: File, username: string, password: string): Observable<HttpEvent<any>> {
// Note - this returns an EVENT, so we can track progress
var headers = new HttpHeaders();
headers.append("Authorization", "Basic " + btoa(`${username}:${password}`));
let formData = new FormData();
formData.append('upload', file);
const params = new HttpParams;
const options = { headers: headers, params: params, reportProgress: true };
const req = new HttpRequest('PUT', 'http://use08nexus01p:8081/nexus/content/repositories/', formData, options);
return this.http.request(req);
}
}
8 Comments
Veda
Thank you for the answer. I will give a try!
Veda
Angular : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:4200' is therefore not allowed access. Angular electron : I am getting 500 server error.
Veda
errror : e {headers: t, status: 500, statusText: "Server Error", url: "use08nexus01p:8081/nexus/content/repositories/test/Veda", ok: false…}error: nullheaders: tmessage: "Http failure response for use08nexus01p:8081/nexus/content/repositories/test/Veda: 500 Server Error"name: "HttpErrorResponse"ok: falsestatus: 500statusText: "Server Error"url: "use08nexus01p:8081/nexus/content/repositories/test/Veda"
dmcgrandle
This is not an angular error. See this: daveceddia.com/… Description of what CORS is: developer.mozilla.org/en-US/docs/Web/HTTP/CORS. Would suggest you search on stackoverflow for the new issue you're running into or start a new question to resolve.
Veda
This solution works perfectly fine! I made some mistake in file while calling the function. I was able to resolve it and this solution to upload file to nexus works as expected . Thanks @dmcgrandle
|