1

I will like to upload files to a firebase hosting site. I have read through the documentation but I'm not able to write the code to make the http request.

Reference:

https://firebase.google.com/docs/reference/hosting/rest/v1beta1/sites.versions/populateFiles https://firebase.google.com/support#upload-files

I have two questions.

  1. When I'm looking at the example of upload files, it shows content-of-file2.gz in the body of the request. Does it means when I upload it, I include the content of file in the body of the request or I have to include the file as an object in the body of the request?
  2. If possible, please provide me the code to accomplish it or let me know what should I write in the header, body etc. I'm using Ionic 6. Below is one of my attempts, but it does not work.
constructor(private http: HttpClient) {
    let header = new HttpHeaders();
    let token = "OAuth2 access token";
    header.set("Authorization", "Bearer " + token);

    const h = { 'Content-Type': 'application/octet-stream', "Authorization": "Bearer " + token }
    header.set("Content-Type", "application/json");
    
    this.getJson().subscribe(data => {
      console.log("File:", data);
      
      http.post(
        "https://upload-firebasehosting.googleapis.com/upload/sites/siteId/versions/versionId/files/fileHash",
        data,
        {
          headers: h
        }
      ).subscribe(data1 => {
        console.log(data1);

      })
    })
  }

  public getJson(): Observable<any> {
    
    return this.http.get("./assets/file.json", {
      observe: 'body',
      responseType: "blob"   // This one here tells HttpClient to parse it as text, not as JSON
    });
  }

Please correct me if I asked wrongly. Thanks.

0 Answers0