1

I'm using angular 5 and making a post request with XML data(as my backend only accepts data in XML format). I want to change the Content-Type of header to application/xml but it always sends request with text/plan. Please look into it and let me know what I'm doing wrong.

Here is my Code

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders, HttpRequest } from '@angular/common/http';
import { Response, Http, RequestOptions ,Headers } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/Rx';
import * as xml2js from 'xml2js';

@Injectable()
export class ApiService {

  constructor( private httpClient: HttpClient, private http: Http ) { }

  signin(){
    let header = new HttpHeaders();
    header.append('Content-Type', 'application/xml');
    header.append('Accept' , 'application/xml');

    console.log("Checking Content-Type:  " , header.get('Content-Type'));

    let body = '<request>' +
                '<username>Ken</username>' +
                '<password>sparks</password>' +
                '</request>';
    return this.httpClient.post('https://Ip:8002/?event=account_login', body , {headers:header});

  }

}

Here is my Network

enter image description here

Adnan Sheikh
  • 559
  • 1
  • 10
  • 23
  • I thought response (not request) header controls the file format. If your server is sending back xml, then all you need to do is to handle xml in response. – windmaomao Mar 07 '18 at 13:22
  • I have tried using responseType, My API is called successfully while using responseType but still I got a response from the backend saying request should be in xml formate. So, I think response and request are different things and we need to handle both of them separately. – Adnan Sheikh Mar 08 '18 at 04:24
  • So this was an issue with the cors, Header was working fine with [Raghu rams answer above](https://stackoverflow.com/a/49153292/4887891) – Adnan Sheikh Aug 28 '18 at 07:10

2 Answers2

1

Try changing your code to

let header = new HttpHeaders();
    header = header.append('Content-Type', 'application/xml');
    header = header.append('Accept' , 'application/xml');
Raghu Ram
  • 141
  • 5
  • I've tried this already. But when I append header like this and pass it in the request it gives a preflight error saying `Response for preflight has invalid HTTP status code 405.` See my other Question [here](https://stackoverflow.com/q/49125638/4887891) – Adnan Sheikh Mar 08 '18 at 04:55
1

HttpHeaders are immutable. https://angular.io/guide/http#update-headers

Try that instead for headers.

let header = new HttpHeaders()
.append('Content-Type', 'application/xml')
.append('Accept' , 'application/xml');

Also, if you don't want response to be parsed as Json automatically, you need to specify responseType

return this.httpClient.post('https://Ip:8002/?event=account_login', body , {headers:header, responseType: 'text'});

https://angular.io/guide/http#requesting-non-json-data

David
  • 28,746
  • 10
  • 68
  • 95
  • I've tried this already. But when I append header like this and pass it in the request it gives a preflight error saying `Response for preflight has invalid HTTP status code 405.` See my other Question [here](https://stackoverflow.com/q/49125638/4887891) – Adnan Sheikh Mar 08 '18 at 04:51
  • Well that's a different question, indeed. But is the content type passed correctly now? 405 means that the server is not configured to handle OPTIONS requests – David Mar 08 '18 at 05:35