question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Force http_get to return content after loading it to the browser

See original GitHub issue

I’m using the following script for getting request and response structures:

function main(splash)
    local url = splash.args.url
    local reply = splash:http_get(url)
    assert(splash:wait(1))
    return {
		    reply.request.info,	
		    reply.info
		}
end

From documentation - splash:http_get method returns content without loading result to the browser. Actually I would need to get content.text as it is returned by splash:go(url) method after loading content to the browser. Is it possible to don’t call two separate methods twice?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kmikecommented, Apr 12, 2017

@slotix I think there are two options:

  1. get response using splash:http_get, then use splash:set_content to load it in a browser, then call splash:html to get the processed version.
  2. call splash:go and use splash:on_response to store raw response body of the first non-redirect response; it will require setting splash.response_body_enabled to true.
0reactions
slotixcommented, Apr 22, 2017

The following code works for me perfectly

function string.ends(String,End)
  return End=='' or string.sub(String,-string.len(End))==End
end
function remove_trailing_slash(text)
  if string.ends(text, "/") then
    text = text:sub(1, -2)
  end
  return text
end

function main(splash)
  local url = splash.args.url
  splash:on_response(function (response)
  url = remove_trailing_slash(url)
  resp_url = remove_trailing_slash(response.info.url)
	if resp_url == url then
    status = response.info.status
    is_redirect = status == 301 or status == 302
    if is_redirect then
      url = response.info.redirectURL
    elseif status == 200 then
      r = response
    end
  end
  end)
  assert(splash:go(url))
  assert(splash:wait(1))
    return {
      request = r.request.info,
      response = r.info,
      html = splash:html(),
    }
end
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to force ASP.NET Web API to return plain text?
You can force it by setting the content type header on httpResponseMessage.Headers to text/plain in your controller provided you have registered ...
Read more >
Using fetch to Send HTTP Requests in JavaScript - Stack Abuse
An HTTP response object that's returned after a successful fetch() requests can be parsed to various formats. In addition to the json() method, ......
Read more >
Redirections in HTTP - MDN Web Docs - Mozilla
When browsers receive a redirect, they immediately load the new URL provided in the Location header. Besides the small performance hit of an ......
Read more >
Format response data in ASP.NET Core Web API
ASP.NET Core MVC supports formatting response data, using specified formats or in response to a client's request.
Read more >
How to make JavaScript wait for a API request to return?
Async: It makes javascript execute promise based codes as if they were synchronous and return a value without breaking the execution thread.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found