0

I'm trying to access an XML file available online, but after the GET the XML format disappears.
What am I doing wrong?
Thank you so much!

func getHttp(address string) string{
    resp, err := http.Get(address)
    resp.Header.Add("Content-Type","application/xml; charset=utf-8")
    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()

    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    return (string(data))
}

The new format looks like this:

{"SessionKey":"229eaeaa9fb14a0d85ff38ae4e0c7870_ecilpojl_018FC93424D13ECC0908CE5BC5E3F86B","Query":{"Country":"GB","Currency":"GBP","Locale":"en-gb","Adults":1,"Children":0,"Infants":0,"OutboundDate":"2016-10-08","LocationSchema":"Default","CabinClass":"Economy","GroupPricing":false},

instead of

<SessionKey>229eaeaa9fb14a0d85ff38ae4e0c7870_ecilpojl_018FC93424D13ECC0908CE5BC5E3F86B</SessionKey>
  <Query>
    <Country>GB</Country>
    <Currency>GBP</Currency>
    <Locale>en-gb</Locale>
    <Adults>1</Adults>
    <Children>0</Children>
    <Infants>0</Infants>
    <OutboundDate>2016-10-08</OutboundDate>
    <LocationSchema>Default</LocationSchema>
    <CabinClass>Economy</CabinClass>
    <GroupPricing>false</GroupPricing>
  </Query>
5
  • The problem is in your main. Show your main function: stackoverflow.com/help/mcve and you don't need: resp.Header.Add("Content-Type","application/xml; charset=utf-8") Commented Oct 2, 2016 at 10:54
  • In my main i just call this function with the web address Commented Oct 2, 2016 at 10:56
  • 1
    So it is JSON not XML Commented Oct 2, 2016 at 11:05
  • If I click on the link the browser generates an xml file, but after the GET it changes Commented Oct 2, 2016 at 11:07
  • The server is sending json based on the request, maybe the user agent in this case. Why not use the json? It will be easier to unmarshal and contain the same data. Commented Oct 2, 2016 at 11:23

1 Answer 1

1

See: https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending

Try this (and replace the web address with yours):

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    fmt.Println(getHttp(`http://stackoverflow.com/`))
}
func getHttp(url string) string {    
    client := &http.Client{}
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        panic(err)
    }
    req.Header.Set("ACCEPT", "application/xhtml+xml,application/xml")
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    return string(data)
}

I hope this helps.

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

2 Comments

Content-Type is for the data in the request but you're not sending any. The Accept header is the standard way to request a data type.
You don't specify the encoding. Just ("Accept", "application/xml")

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.