3

I'm am new to angular and using angular 5.

I'm making a http post request and I want to send xml data to backend as my backend only accept data in xml format.

I've followed some tutorials but couldn't get it to work. Please let me know what I'm doing wrong and how can I make it work.

This is how I'm making request.

signin(){
    const headers = new HttpHeaders({responseType: 'text' , 'Content-Type': 'text/xml' }).set('Accept', 'text/xml');
    let body =  '<request>' 
                '<username>Username</username>' 
                '<password>Password</password>' 
                '</request>';
    const hdr = {headers:headers , body:body};
    console.log("Checking:  " , hdr);
    return this.http.post('https://66.128.132.126:8002/?event=account_login' , hdr);
}

Here is my console for this: Here is My Console for this

And here is my network: Here is My network

Adnan Sheikh
  • 559
  • 1
  • 10
  • 23

2 Answers2

2

the responseType and body should not be appended with headers. body should be sent separetly and with headers and responseType options object should be formed and sent as a parameter to post.

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

    return this.http.post('https://66.128.132.126:8002/?event=account_login',body , { headers: headers, responseType: 'text' });
}
Raghu Ram
  • 141
  • 5
  • Still getting the preflight error `Response for preflight has invalid HTTP status code 405` – Adnan Sheikh Mar 08 '18 at 05:14
  • have you authorized the OPTIONS requests in your backend. similar to POST , GET. – Raghu Ram Mar 08 '18 at 05:31
  • I don't have access to backend. But I'm sending a POST request, I don't understand why it is going with OPTIONS. Can you please put some light on it? – Adnan Sheikh Mar 08 '18 at 09:51
  • 1
    preflight request are made automatically by browser if requests are across different origins. It cannot be prevented. The only way to solve this is to request the backend people to allow OPTIONS request. you can see more detailed explantion here (https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it) – Raghu Ram Mar 08 '18 at 10:06
1

By default Angular CLI made a Preflight Request in order to check CORS: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

It seems that you backend isn't handling this request correctly. We need backend info to help you

Jose A. Matarán
  • 470
  • 2
  • 7
  • 16