-3

how can I make HTTP Requests under Ruby with modiefied header?

I want to add attributes like "PublicKey" and "Accept".

And I want add a new authentication header value like "Basic".

My approach:

uri = URI('https://www.blablubbxxx0.com/')
Net::HTTP.get(uri)
.
.
.
.
.
.
.
.
r.az91
  • 41
  • 1
  • 4
  • 1
    https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#class-Net::HTTP-label-Setting+Headers – CBroe Apr 18 '17 at 13:40
  • thank you for your answer, but i dont understand, how i can use these informations – r.az91 Apr 18 '17 at 13:52
  • Why not? What exactly is the problem? – CBroe Apr 18 '17 at 13:57
  • 1
    @r.az91 did you read the link because amazingly both your questions were answered by that link without the need to even scroll. "Setting Headers" and "Basic Authentication" are right there and I don't mean to sound crass but if that part is not understandable is it possible you might be a little too new ruby to really be trying to make Net requests? – engineersmnky Apr 18 '17 at 15:24
  • Possible duplicate of [How to make an HTTP GET with modified headers?](http://stackoverflow.com/questions/587559/how-to-make-an-http-get-with-modified-headers) – Carter Brainerd Apr 18 '17 at 16:48
  • Please read "[ask]" and "[mcve]" and the linked pages, along with "[How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/questions/261592)". We expect evidence of your effort in a question, where did you search and why didn't it help? What did you try and why didn't it work? Your example shows no such effort. Throwing up your hands and asking without that evidence is a good way to get your question closed as off-topic. As far as Net::HTTP, there are better libraries for writing clients. Net::HTTP is pretty low-level. – the Tin Man Apr 18 '17 at 18:46

1 Answers1

0

Instead of declaring the request as such: Net::HTTP.get(uri), use .new.

So a rewrite of your code would be:

uri = URI.parse('https://www.blablubbxxx0.com/')
req = Net::HTTP::Get.new(uri.path)
req.add_field("HeaderName", "HeaderValue")    # Adding a header
req['If-Modified-Since'] = file.mtime.rfc2822 # Setting a header


res = Net::HTTP.new(url.host, url.port).start do |http|
  http.request(req)
end

Code from https://dzone.com/articles/send-custom-headers-ruby

Carter Brainerd
  • 199
  • 2
  • 10
  • thank you for your help. i get a message: "Connection reset by peer". how can I solve these Problem? – r.az91 Apr 19 '17 at 12:32
  • See [here](http://stackoverflow.com/questions/1434451/what-does-connection-reset-by-peer-mean) to read more about the connection reset by peer error. It could be that the server only accepts certain headers and is denying your connection. Also, be sure that port 80/443 is open on the target you're trying to connect to. – Carter Brainerd Apr 19 '17 at 13:27