Encountering an HTTP parsing failure while sending XML through Angular 5's HttpClient

Struggling to access a local webservice through XML:

Take a look at the code below:

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 specific error message received reads:

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

Trying to identify and resolve the issue - Any suggestions?

Answer №1

Have you experimented with this code snippet?

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

It is important to note that the responseType must be specified as text not only in the headers but also in the object httpOptions. Failure to do so may result in Angular incorrectly parsing the response as JSON.

Answer №2

Make a modification to your request like so:

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

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

            console.log(result);
        })
    },error => {
            console.log('An error occurred: ', error);
        }
    );
}

Answer №3

Experiment

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

Instead of using template string with whitespace char "new line", try the traditional method with regular strings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Turn off the scrolling bars and only allow scrolling using the mouse wheel or touch scrolling

Is there a way to only enable scrolling through a webpage using the mouse wheel or touch scrolling on mobile devices, while disabling browser scroll bars? This would allow users to navigate up and down through div elements. Here is the concept: HTML: &l ...

Troubleshooting Guide: Issues with Bootstrap 3 Modal Window Implementation

This question is so simple that it's embarrassing. I attempted to copy the code from http://getbootstrap.com/javascript/#modals directly into a basic page setup, but it's not functioning. It seems like I'm making a very silly mistake. Here i ...

React 18 update causes malfunctioning of react-switch-selector component

I'm facing an issue where the component is not rendering. I attempted to start a new project but it still didn't work. Is there a solution to fix this problem or should I just wait for an update from the original repository? Encountered Error: ...

Is it possible to integrate a jQuery function within PHP code when encountering a "is not defined"

I am looking to implement the jQuery-confirm dialog in place of using a JavaScript alert generated by PHP. However, when I execute the following code, I encounter the following error message: $ is not defined echo "<script>$.alert({title: ' ...

Unusual reverberation following the completion of a foreach loop for combining a series of

Hi everyone, I've been struggling for the past 30 minutes trying to figure out why I'm getting a strange output. The variable outside of the loop is displaying one value, while inside it's working fine. Can someone please shed some light on ...

A guide on organizing nested object data in react-native

{ "crocodile":{ "age_in_years":"20", "name":"snapjaw", "country":"australia" }, "jaguar":{ "age_in_years":"8", "name&q ...

Enabling individuals to transfer their content to Amazon S3

I have set up an S3 bucket named BUCKET in region BUCKET_REGION. I want to enable users of my web and mobile apps to upload image files to this bucket, with specific restrictions based on Content-Type and Content-Length (specifically, only allowing jpegs u ...

Looking to utilize vue.js to alter the color of the <li> element when a select option is chosen

I'm a beginner in vue.js and I'm attempting to change the background color by using the select option. Despite trying the cueCardsColor method, nothing seems to be happening. <ul> <li :class="+ cueCardColor"> <sele ...

"Utilize a loop in Angular 2 to consistently send HTTP GET requests to service

Hello, I'm new to working with Angular. Currently, I have an array of product IDs and I would like to make HTTP GET requests based on each ID in the array using a loop. Can someone assist me with this task? Service : addedProductIdArray : string[] = ...

Incomplete header data in Angular $resource GET request

Currently, I am working with Ionic 1.3 and Angular 1.5. My goal is to retrieve some header properties from my response. The code snippet I am using looks something like this: factory('Service', function($resource, API_SETTINGS, JsonData) { re ...

Attempting to create a Next.js 13 application, but struggling with using the client-side functionality

Exploring Next.js for the first time, I embarked on creating a simple application. Everything was going smoothly until I attempted to include a "use client" tag at the beginning of a component to utilize certain hooks. This resulted in the app breaking and ...

Issue with converting form data to JSON format

Having an issue converting a filled form in HTML to a JSON request for sending to the server via HTTP POST. Despite having a filled form, the JSON request only shows an empty array. Here is the JavaScript snippet: $("#submitSurveyBtn").on("click", functi ...

Utilizing the Flex property on the parent div ensures that all child elements receive an equal width

I am currently working on a messaging application and I want the layout to be similar to Twitter or Whatsapp. Specifically, I want the messages from the person the user is chatting with to appear on the left side, while the user's own messages should ...

fade out row upon successful deletion using ajax

My code includes an AJAX function to delete items by row. In my table, there is a column with the action to perform the deletion. Here is the AJAX function: //delete detail function deleteDetail(id_po_req_detail) { var tr = ...

Choose the text by clicking on a button

I currently have: <span class="description">Description 1</span> <button class="select">Select!</button> <span class="description">Description 2</span> <button class="select">Select!</button> <span clas ...

After redirection to a new page, the Ionic-vue menu malfunctioned

I have developed a reusable header component named Header, which contains an IonMenu component for consistency across pages. The menu is associated with the IonRouterOutlet as per the documentation, but I am encountering an issue when navigating between pa ...

The ckeditor vanishes upon refreshing the div element

I have created two pages named openclosediv.php and content.php. The openclosediv.php page contains a list of records and a button that can show/hide the div, bringing in the content from content.php. However, I am facing an issue where the CKEditor in the ...

Avoiding code execution by injections in Javascript/Jquery

Currently, I'm fetching JSON data for a .getJSON function in Jquery. To ensure the data's security, I am considering using .text (I believe this is the correct approach). The JSON has been successfully validated. Below is the script that I am cu ...

Removing items in vue.js

I'm currently in the process of learning Vue.js. This is my first attempt at creating a small to-do application, and I am encountering issues with deleting each individual task upon clicking. Despite watching multiple YouTube tutorials, I have not bee ...

Angular: facing difficulty displaying static html pages on server, although they render correctly when run locally

Within my Angular project, I have stored a few static html files (specifically sampleText.html) in the src/assets/html folder. In one of my components, I am attempting to fetch and display this file. The following code is being used for this purpose. Every ...