user9597092
user9597092

Reputation:

Angular 5 - HttpClient XML Post - Http failure during parsing

I am trying to login on a local webservice using xml:

Here is the code:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'text/xml',
    'Accept':  'text/xml',
    'Response-Type': 'text'
  })
};


login() {
    const postedData = `
        <authenticationDetail>
            <userName>myusername</userName>
            <password>mypassword</password>
        </authenticationDetail>`;
    return this.http.post('http://localhost/login.ws', postedData, httpOptions)
    .subscribe(
        result => {
            console.log('This result is' + result);
        },
        error => {
            console.log('There was an error: ', error);
        }
    );
}

The error I'm getting is:

Http failure during parsing for 'http://localhost/login.ws'

What is the problem here? How can I fix this?

Upvotes: 0

Views: 1321

Answers (3)

Avram Virgil
Avram Virgil

Reputation: 1210

Try and change your request to this:

login(){ 
const headers = new HttpHeaders(); 
headers = headers.append('Content-Type': 'text/xml'); 
headers = headers.append('Accept', 'text/xml'); 
let body = '<authenticationDetail>' 
           '<username>Username</username>' 
           '<password>Password</password>' 
           '</authenticationDetail>'; 

return this.http.post('localhost/login.ws',body , { headers: headers, responseType: 'text' })
.subscribe(
        res => {
        parseString(res.text(), (err, result) => {
            if (err) {
                return console.log('invalid XML');
            }

            console.log(result);
        })
    },error => {
            console.log('There was an error: ', error);
        }
    );
}

Upvotes: 0

Franziskus Karsunke
Franziskus Karsunke

Reputation: 5208

Have your tried

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'text/xml',
        'Accept':  'text/xml',
        'Response-Type': 'text'
    }), 
    responseType: 'text'
};

The responseType has to be set to text not only in the headers but also in httpOptions. Otherwise Angular will parse the response of the call as JSON.

Upvotes: 1

justMe
justMe

Reputation: 664

Try

const postedData = '<authenticationDetail>' +
                   '<userName>myusername</userName>' +
                   '<password>mypassword</password>' +
                   '</authenticationDetail>';

Template string adds whitespace char "new line", try the classic approach with regular strings.

Upvotes: 0

Related Questions