2

When I try to upload a base64 image inside my angular application, the http.post method throws an error:

Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString.

This is what the code looks like:

let headers = new Headers();
      headers.append("Accept", 'application/json');
      headers.append("Content-Type", "application/json");
      headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/68.0' );

      let postData = {
        "AudometerCapture":this.abc,                          
        "Door1":this.abc,
        "Door2":this.abc,
       "Door3":this.abc,
        "Door4":this.abc,
       "TransactionID": 90
    }

      this.http.post('http://apiearningwheels.sharpnettechnology.com/api/DailyImageUpload/UploadDailyImages', JSON.stringify(postData), {headers: headers})
        .map(res => res.json())
        .subscribe(data => {
          console.log(data);
          this.showLongToast("result is :- " + res);
        });

i expect the output to be 'result is = ' but i get the error.

The request works correctly using Postman.

qristjan
  • 183
  • 5
Dipika
  • 39
  • 1
  • 1
  • 5
  • Are your headers correctly defined? (use this as an example: https://stackoverflow.com/questions/47757655/how-to-add-headers-to-my-angular-post-request) – qristjan Sep 11 '19 at 11:32
  • i add the header which is necessary,. If u have any idea then tell me what is the problem. – Dipika Sep 11 '19 at 11:44

1 Answers1

5

The error is thrown because there is a symbol for three dots (…) in the last header, which cannot be correctly converted to ByteString.

So you have to remove the line or change it like this:

headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/68.0' );

to

headers.append('User-Agent', 'Mozilla/5.0 (Windows NT 10.0;) Gecko/20100101 Firefox/68.0' );

I tested the headers, using https://codesandbox.io/s/angular, and it threw me an error on that line: Cannot convert string to ByteString because the character at index 30 has value 8230 which is greater than 255.

qristjan
  • 183
  • 5