curlchrome-remote-debugging

Use cUrl with Chrome remote debugging


I'm trying to connect to Chrome remote debuging with cUrl, however, responses from Chrome return "200 OK" but no data.

I set up chrome using:

chrome.exe --headless --remote-debugging-port=12345

I can successfully get a list of pages with:

>curl -i "http://localhost:12345/json"

HTTP/1.1 200 OK
Content-Length:612
Content-Type:application/json; charset=UTF-8
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575",
  ...
   "webSocketDebuggerUrl": "ws://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575"
} ]

However, I can only get empty successful response from debugger URLs:

>echo {"id":0,"method":"Page.navigate","params":{"url":"https://stackoverflow.com/"}}|curl -i "http://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575" -H "Content-Type: application/json" -d -

HTTP/1.1 200 OK
Content-Length:0
Content-Type:text/plain

Commands like Page.navigate and also misformed requests return 200 OK but nothing is performed.

What I'm I missing???


Solution

  • You need to use websockets to manage chrome like that. Quoting chromedevtools.github.io:

    Your application can discover available pages by requesting: http://localhost:9222/json and getting a JSON object with information about inspectable pages along with the WebSocket addresses that you could use in order to start instrumenting them

    This is how it would work (at least on my Mac):

    Run Chrome

    docker pull deepsweet/chromium-headless-remote:69
    docker run -it --rm -p 9222:9222 deepsweet/chromium-headless-remote:69
    

    Fetching WebSocket addresses

    curl -i "http://localhost:9222/json"
    HTTP/1.1 200 OK
    Content-Length:361
    Content-Type:application/json; charset=UTF-8
    
    [ {
       "description": "",
       "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
       "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
       "title": "about:blank",
       "type": "page",
       "url": "about:blank",
       "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
    } ]
    

    Sending your command

    echo '{ "id":2, "method":"Page.navigate", "params":{"url": "http://www.stackoverflow.com"} }' | websocat -t - ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF
    

    websocat is used in the example. You can also write a simple script to do it like here

    Make sure the page is opened

    curl -i "http://localhost:9222/json"
    HTTP/1.1 200 OK
    Content-Length:432
    Content-Type:application/json; charset=UTF-8
    
    [ {
       "description": "",
       "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
       "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
       "title": "Stack Overflow - Where Developers Learn, Share, & Build Careers",
       "type": "page",
       "url": "https://stackoverflow.com/",
       "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
    } ]