naomi
naomi

Reputation: 21

Intercepting responses to a process in golang

I have a process that activates a browser, which makes a request to a local server. The server should respond but I do not know how to see, client side, the answer. I need that is the browser that makes the request. I do not want to write it myself with http.NewRequest.

client.go:

func openChrome() {

  var page = "https://localhost:1333/"

  program := "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
  url := []string{"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", page}
  attr := &os.ProcAttr{
    Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
  }

  proc,err:=os.StartProcess(program, url, attr)
  proc.Wait();

  // if err!=nil, log.Fatal(err)
}

Upvotes: 1

Views: 655

Answers (1)

Kenny Grant
Kenny Grant

Reputation: 9623

To see the response and have easier control over requests could use a tool like chromedp, chromedriver (with webdriver) or selenium to load pages and find the response in the browser. All of these should be accessible to varying degrees from go, and can be used to drive the browser to go through the standard request cycle as if a human were doing it (to load content and query what is loaded).

You can see the stdout and stderr from a process you launch but that is unlikely to help you for this particular task so I'm ignoring that.

You don't give a reason for excluding a direct request, but for full control this would be another possibility, and unless you need to render html your code can do everything a browser could do (change user agent, parse html etc).

Upvotes: 1

Related Questions