1

The Http get request returns and xsd file content, below is the code snippet

headers = headers.append('Content-Type', 'text/plain');
        headers = headers.append('responseType', 'text');
        headers = headers.append('Accept', 'application/octet-stream');

retun this.http.get(data[0]._links['http://identifiers.emc.com/content-download'].href, { headers: headers }))
      .map((response: Response) => {
        let data: any;
        data = response;
      return content;
      })

The code fails with the error

"SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse" Http failure during parsing for "request url"

I have added the "requestType:text" to the header, am I missing something, why it is trying to parse thinking the response is of JSON ?

below are the snapshots of request and response headers

Response header Request header

Below is the sample of response

 <!--
      ~ Copyright (c) OpenText Corporation. All Rights Reserved.
      -->
    <xs:schema elementFormDefault="qualified" targetNamespace="urn:eas-samples:en:xsd:phonecalls.1.0" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Calls">
            <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                    <xs:element name="Call">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="SentToArchiveDate" type="xs:date"/>
                                <xs:element name="CallStartDate" type="xs:dateTime"/>
                                <xs:element name="CallEndDate" type="xs:dateTime"/>
                                <xs:element name="CallFromPhoneNumber">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:positiveInteger">
                                            <xs:minInclusive value="1"/>
                                            <xs:totalDigits value="11"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:element
Divya Neelaiah
  • 59
  • 1
  • 1
  • 6
  • Possible duplicate of [How to parse xml in Angular 2](https://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2) – Fals Mar 22 '18 at 10:59

3 Answers3

0

I think you can use a module to parse your xml response or use a module that convert xml to json.

Here are 2 modules you can use:

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

you have to set the responseType, because the default one is json, you are only specifying the request headers: Take a look at the HttpRequest documentation: https://angular.io/api/common/http/HttpRequest The response type can be

'arraybuffer' | 'blob' | 'json' | 'text'

I think you would need sth like this:

this.http.get(data[0]._links['http://identifiers.emc.com/content-download'].href, { headers: headers, responseType: 'text' }))
  .map((response: Response) => {
    let data: any;
    data = response;
  return content;
  })
lama
  • 362
  • 1
  • 2
  • 9
0

I too had similar issue , for me solution was to handle response type both at angular and api side.

Angular

return this.http.post(this.resourceUrl+'/imageUpload', copy, {responseType: 
'text'})

Java

@PostMapping(value = "/questions/imageUpload",produces = { "text/plain" })
sandesh dahake
  • 1,028
  • 7
  • 16