How to get response after http request
I'm studying Go and am a real newbie in this field.
I am facing a problem when I try to copy some value.
What I am doing is:
- I want to get some response in [response] using httpRequest.
httpClient := &http.Client
response, err := httpClient.Do(req)
if err != nil
panic(err)
- After that, I want to save the stored value in response at 'origin.txt'
origin_ ,_:= ioutil.ReadAll(response.Body)
f_, err := os.Create("origin.txt")
f_.Write(origin_);
- And I want to get a specific value by using goquery package.
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil
log.Fatal(err)
doc.Find(".className").Each(func(i int, s *goquery.Selection)
w.WriteString("============" + strconv.Itoa(i) + "============")
s.Find("tr").Each(func(i int, s_ *goquery.Selection)
fmt.Println(s_.Text())
w.WriteString(s_.Text())
)
)
But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).
At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.
So I tried to copy it to another object and then do it again.
origin := *response
but, I got the same result as first.
What should I do?
How can I assign a reference value to another one by its value?
Should I request it twice for each attempt?
go goquery
add a comment |
I'm studying Go and am a real newbie in this field.
I am facing a problem when I try to copy some value.
What I am doing is:
- I want to get some response in [response] using httpRequest.
httpClient := &http.Client
response, err := httpClient.Do(req)
if err != nil
panic(err)
- After that, I want to save the stored value in response at 'origin.txt'
origin_ ,_:= ioutil.ReadAll(response.Body)
f_, err := os.Create("origin.txt")
f_.Write(origin_);
- And I want to get a specific value by using goquery package.
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil
log.Fatal(err)
doc.Find(".className").Each(func(i int, s *goquery.Selection)
w.WriteString("============" + strconv.Itoa(i) + "============")
s.Find("tr").Each(func(i int, s_ *goquery.Selection)
fmt.Println(s_.Text())
w.WriteString(s_.Text())
)
)
But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).
At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.
So I tried to copy it to another object and then do it again.
origin := *response
but, I got the same result as first.
What should I do?
How can I assign a reference value to another one by its value?
Should I request it twice for each attempt?
go goquery
add a comment |
I'm studying Go and am a real newbie in this field.
I am facing a problem when I try to copy some value.
What I am doing is:
- I want to get some response in [response] using httpRequest.
httpClient := &http.Client
response, err := httpClient.Do(req)
if err != nil
panic(err)
- After that, I want to save the stored value in response at 'origin.txt'
origin_ ,_:= ioutil.ReadAll(response.Body)
f_, err := os.Create("origin.txt")
f_.Write(origin_);
- And I want to get a specific value by using goquery package.
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil
log.Fatal(err)
doc.Find(".className").Each(func(i int, s *goquery.Selection)
w.WriteString("============" + strconv.Itoa(i) + "============")
s.Find("tr").Each(func(i int, s_ *goquery.Selection)
fmt.Println(s_.Text())
w.WriteString(s_.Text())
)
)
But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).
At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.
So I tried to copy it to another object and then do it again.
origin := *response
but, I got the same result as first.
What should I do?
How can I assign a reference value to another one by its value?
Should I request it twice for each attempt?
go goquery
I'm studying Go and am a real newbie in this field.
I am facing a problem when I try to copy some value.
What I am doing is:
- I want to get some response in [response] using httpRequest.
httpClient := &http.Client
response, err := httpClient.Do(req)
if err != nil
panic(err)
- After that, I want to save the stored value in response at 'origin.txt'
origin_ ,_:= ioutil.ReadAll(response.Body)
f_, err := os.Create("origin.txt")
f_.Write(origin_);
- And I want to get a specific value by using goquery package.
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil
log.Fatal(err)
doc.Find(".className").Each(func(i int, s *goquery.Selection)
w.WriteString("============" + strconv.Itoa(i) + "============")
s.Find("tr").Each(func(i int, s_ *goquery.Selection)
fmt.Println(s_.Text())
w.WriteString(s_.Text())
)
)
But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).
At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.
So I tried to copy it to another object and then do it again.
origin := *response
but, I got the same result as first.
What should I do?
How can I assign a reference value to another one by its value?
Should I request it twice for each attempt?
go goquery
go goquery
edited Nov 16 '18 at 8:34
poy
6,55263465
6,55263465
asked Nov 16 '18 at 6:43
JungHoonJungHoon
1581313
1581313
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I actually don't see where you use shared resources between 2 and 3.
However that being said origin := *response
won't buy you much. The data (response.Body
) is a io.ReadCloser
. The ioutil.ReadAll()
will consume and store all the data that the stream has. You only get to do this once.
However you have the data stored in origin
. If you need another io.Reader
for that data (say for case 3), then you can make that byte slice look like an io.Reader
again: bytes.NewReader(origin)
.
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just usebytes.NewReader(origin)
as I suggested then.
– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332721%2fhow-to-get-response-after-http-request%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I actually don't see where you use shared resources between 2 and 3.
However that being said origin := *response
won't buy you much. The data (response.Body
) is a io.ReadCloser
. The ioutil.ReadAll()
will consume and store all the data that the stream has. You only get to do this once.
However you have the data stored in origin
. If you need another io.Reader
for that data (say for case 3), then you can make that byte slice look like an io.Reader
again: bytes.NewReader(origin)
.
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just usebytes.NewReader(origin)
as I suggested then.
– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
add a comment |
I actually don't see where you use shared resources between 2 and 3.
However that being said origin := *response
won't buy you much. The data (response.Body
) is a io.ReadCloser
. The ioutil.ReadAll()
will consume and store all the data that the stream has. You only get to do this once.
However you have the data stored in origin
. If you need another io.Reader
for that data (say for case 3), then you can make that byte slice look like an io.Reader
again: bytes.NewReader(origin)
.
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just usebytes.NewReader(origin)
as I suggested then.
– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
add a comment |
I actually don't see where you use shared resources between 2 and 3.
However that being said origin := *response
won't buy you much. The data (response.Body
) is a io.ReadCloser
. The ioutil.ReadAll()
will consume and store all the data that the stream has. You only get to do this once.
However you have the data stored in origin
. If you need another io.Reader
for that data (say for case 3), then you can make that byte slice look like an io.Reader
again: bytes.NewReader(origin)
.
I actually don't see where you use shared resources between 2 and 3.
However that being said origin := *response
won't buy you much. The data (response.Body
) is a io.ReadCloser
. The ioutil.ReadAll()
will consume and store all the data that the stream has. You only get to do this once.
However you have the data stored in origin
. If you need another io.Reader
for that data (say for case 3), then you can make that byte slice look like an io.Reader
again: bytes.NewReader(origin)
.
edited Nov 16 '18 at 7:10
answered Nov 16 '18 at 6:51
poypoy
6,55263465
6,55263465
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just usebytes.NewReader(origin)
as I suggested then.
– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
add a comment |
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just usebytes.NewReader(origin)
as I suggested then.
– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
oops sorry. i changed my question... Sorry. The problem was at [response] object!
– JungHoon
Nov 16 '18 at 7:02
Perfect, well just use
bytes.NewReader(origin)
as I suggested then.– poy
Nov 16 '18 at 7:11
Perfect, well just use
bytes.NewReader(origin)
as I suggested then.– poy
Nov 16 '18 at 7:11
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!
– JungHoon
Nov 16 '18 at 8:23
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
I'm glad it worked for you!
– poy
Nov 16 '18 at 8:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332721%2fhow-to-get-response-after-http-request%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown