Parse text/plain response

Hi guys, I would like to know how can I parse an text/plain response, let’s imagine that the curl of the page http://test.com is

123abc
456.12.3
789.45.12
78.45.12

how can I create an array that contains all the values of the response Without the first line in this example 123abc?
Thanks for your help

This response does not look like HTML but like text/plain. Am I correct?

1 Like

Doesn’t look like HTML, but strings.Split() will help you.

2 Likes

yes.

So the things I should do is to make an http.Get() and save it in a var, defer it and than use strings.Split()?

yes. It could be something like

url := "http://test.com"
response, err := http.Get(url)
if err != nil {
	log.Fatal(err)
}
defer response.Body.Close()

responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
	log.Fatal(err)
}

responseString := string(responseData)
items := Split(responseString, ",")
for str:= range items {
   fmt.println(str)
}
1 Like

what about removing the first line?
thx you all a lot m8s for your help :slight_smile:

I miss out a _ in the previous post. To exclude the first element do something as :slight_smile:

allExceptFirst := items[1:]
for  _, str := range allExceptFirst {
	fmt.Println(str)
}

There’s also the possibility that response.Body is nil without the call returning an error. Better check that as well :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.