2

I would like to send image with http.post. I use that code block , but gives me 404 error. I tried api on postman , return success.

Where is my wrong?

Thanks.

changeprofileimg()
  {
    this.camera.getPicture({
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.DATA_URL,
      quality: 100,
      encodingType: this.camera.EncodingType.PNG,
      targetHeight:500,
      targetWidth:500,
      correctOrientation:true
    }).then(imageData => {

      const image = 'data:image/jpeg;base64,'+imageData;   
      var veri;   
      var headers = new Headers();
      headers.append('Accept', 'application/json');
      headers.append('Authorization' , 'Bearer '+this.globalvars.getToken());
      headers.append('Content-Type', 'multipart/form-data');
      let options = new RequestOptions({ headers: headers });


      let postParams = {
        file:image
      }

      this.http.post("http://.../api/profile/image/upload", postParams, options)
        .subscribe(data => {
          console.log( data['_body']);
          veri=data['_body'];
          veri = veri.replace(/\\/g, "");
          veri = JSON.parse(veri);
          console.log(veri);
        });

    });
  } 
Ahmet emre CETIN
  • 321
  • 1
  • 4
  • 22

1 Answers1

4

You should use FormData for this purpose:

      let formData = new FormData();
      formData.append('file', image);


      this.http.post("http://.../api/profile/image/upload", formData, options)
        .subscribe(data => {
          console.log( data['_body']);
          veri=data['_body'];
          veri = veri.replace(/\\/g, "");
          veri = JSON.parse(veri);
          console.log(veri);
        });

    });

Besides that as you're getting an 404 error, so you should double check your api endpoints and rest api methods.

asmmahmud
  • 4,844
  • 2
  • 40
  • 47