Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raw output (to String or ...) #15

Closed
ghost opened this issue Jun 21, 2018 · 9 comments
Closed

raw output (to String or ...) #15

ghost opened this issue Jun 21, 2018 · 9 comments

Comments

@ghost
Copy link

ghost commented Jun 21, 2018

Are you funny to implement that the complete response (with headers, 200 OK, ... etc.) is completely copyied to a String? I would need this to read all the bytes my server sends, like cookies too and output it myself with p, pp or simply puts :)

I mean, actually we got this via puts:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<body>
  <!-- ... -->
</body>
</html>

and i need this:

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<body>
  <!-- ... -->
</body>
</html>

prefered as String :)

This were nice 👍

@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

i dont know about this is or not a feature, but for now you can add a patch to your code:

class Halite::Response
  def print_debug
    puts "#{version} #{status_code} #{status_message}"
    headers.each do |k, v|
      puts "#{k}: #{v.join(", ")}"
    end
    puts
    puts self
  end
end

url = "https://httpbin.org/get"
r = Halite.get(url)
r.print_debug

output:

HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.8.1
Date: Fri, 22 Jun 2018 03:08:03 GMT
Content-Type: application/json
Content-Length: 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur

{"args":{},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"Halite/0.3.2"},"origin":"60.206.194.34","url":"https://httpbin.org/get"}

@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

Or write your custom logger and enable in each request.

@ghost
Copy link
Author

ghost commented Jun 22, 2018

Wow, you are very fast providing this, thank you 👍

Firstly, the output seems as expected, but this way its morely a cosmetic output to see this like raw.

This is because of your puts - this inserts self-generated newlines after the header key-value-pairs and between headers and body and helps viewing it, but what i exactly mean is the response buffer (chunks chained) directly to String; it should be the real raw response, like

HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nServer: gunicorn/19.8.1\r\n...

because sometimes it´s very needed to know whats exactly comes in (or back) :-)

The custom logger would do the same as it did not catch the original response?

Thank you very much - it´s already a very good starting point :-)

@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

Let's improve the code 😄

class Halite::Response
  def dump
    String.build do |io|
      io << version << " " << status_code << " " << status_message << "\n"
      headers.each do |k, v|
        io << k << ": " << v.join(", ") << "\n"
      end

      io << "\n"
      io << to_s
    end.to_s
  end
end

url = "https://httpbin.org/get"
r = Halite.get(url)
puts r.dump

Is it you want?

@ghost
Copy link
Author

ghost commented Jun 22, 2018

I do not dare to answer :)

...no... because it´s adding manually \n again, and i think version, status_code and so on are already always parsed Strings.

But i did not dive deep enough into your source; maybe possible that getting this is not possible if Crystals stdlib always returns already parsed strings. Maybe you got always Strings from their methods and this is the rawest of raw :)

If the last one is correct, than you have already saved me a lot of time because i can reduce doing things in the browser to a minimum - thats really awesome 👍 💯

@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

You are right. all crystal give api is wrapped and it will auto Compression and encoding, check it: https://crystal-lang.org/api/0.25.0/HTTP/Client.html

@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

BTW, Crystal also give a method to returns raw by splice but Halite not delegate it: https://github.com/crystal-lang/crystal/blob/master/src/http/client/response.cr#L63

@ghost
Copy link
Author

ghost commented Jun 22, 2018

Wow - thanks!

Ok, so - i am already very happy with your help and Halite - both were awesome for saving time 🥇

What do you think about merging above to your master? I think, print_debug or dump are always nice to have 👍

Let's go with Halite :)

@ghost ghost closed this as completed Jun 22, 2018
@icyleaf
Copy link
Owner

icyleaf commented Jun 22, 2018

Done, use to_raw to dump it.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant