0

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?

3
  • Is this error reported when you make the request to the server or is the the response value of the subscribe. Commented May 8, 2018 at 11:30
  • It's displayed after: 'There was an error: ' ... HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: etc Commented May 8, 2018 at 11:42
  • Added the code change as an answer because comments are really poor formatted. See below. Commented May 8, 2018 at 12:27

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What das login.ws return? Did you check the network tab? Is it an empty response or is there some binary data in the response?
0

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);
        }
    );
}

1 Comment

I think it is a parse problem, as it is hard to diagnose, but check the code above I edited it to add a parse on the response, try both: parseString(res.text(), or parseString(res, as I do not remember if by asking for a text response will need a .text() on the response.
0

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.

1 Comment

No, that made no difference in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.