# New HTTP Request/Response Help

**URL:** <https://forums.sketchup.com/t/new-http-request-response-help/36794>\
**Category:** Ruby API\
**Tags:** make-2017\
**Created:** [January 4, 2017, 3:26pm UTC](https://forums.sketchup.com/t/new-http-request-response-help/36794 "2017-01-04T15:26:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewBirckbichler](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/matthewbirckbichler/32/19770_2.png) [@matthewBirckbichler](https://forums.sketchup.com/u/matthewBirckbichler)\
**Post date:** [January 4, 2017, 3:26pm UTC](https://forums.sketchup.com/t/new-http-request-response-help/36794/1 "2017-01-04T15:26:09Z")

</div>

I’m just messing with the new HTTP::Request/Response Classes for the first time, and what I’m finding is that the callback block for the start method will not run asynchronously, making the following example impossible because the loop will run forever, preventing the variable ‘status’ from being set:

```
status = nil
request = Sketchup::Http::Request.new(url)
request.start{|req,res|
	status = res.status_code
}
loop{break if !status.nil?}
if status==200
	return true
else
	return false
end

```

maybe I’m just going about this the wrong way?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 4, 2017, 8:43pm UTC](https://forums.sketchup.com/t/new-http-request-response-help/36794/2 "2017-01-04T20:43:10Z")

</div>

Yea, you cannot run a loop like this in event driven embedded Ruby.

**(1)** The docs for this class’ constructor, clearly state:

> _Keeping a reference to the request is necessary in order to ensure the use of the response._

“_Keeping a reference_” means using the correct type of reference (_also loosely called a variable_,) that will not go out of scope and get garbage collected by Ruby’s housekeeping.

Local variables are temporary references and go out of scope quickly, such as when the method wrapping around them, finishes executing. When that happens, (the method ends) the `Sketchup::Http::Request` instance object is no longer referenced, and will get GC’d.

If you have the code above in a script, at the top level, Ruby destroys local references when the script is finished loading. This safeguard is built-into the `load()` and `require()` methods, to prevent local variables from propagating into every object, (like everyone else’s classes and modules,) because the TOPLEVEL\_BINDING, is `Object`.

So you need to use a persistent reference, such as a instance variable (ie, `@request`,) or a class/module variable (ie, `@@request`.)

* * *

**(2)** You do not need to implement a looping construct, in order to watch the object for a responce.

It is already built-into the class. The `start` method registers a block of callback code, to be executed when a HTTP Response has been received.

There needs to be a way better example, rather than having to stitch together all the method examples.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 5, 2017, 4:35pm UTC](https://forums.sketchup.com/t/new-http-request-response-help/36794/3 "2017-01-05T16:35:31Z")

</div>

> [@DanRathbun](#):
>
> There needs to be a way better example, rather than having to stitch together all the method examples.

Logged an issue to add tutorial and example:

> <https://github.com/SketchUp/sketchup-ruby-api-tutorials/issues/1>
>
> Add a Tutorial and Example for using the
> \`Sketchup::Http::Request\` and \`Sketchu…p::Http::Response\` classes.
> 
> The little blurbs of sample code in each method, do not stitch together well into an example.
> 
> See Ruby docs for standard lib class \*\*\`Net::HTTP\`\*\* for example ideas:
> http://ruby-doc.org/stdlib-2.2.4/libdoc/net/http/rdoc/Net/HTTP.html
> 
> \_Ref public forum thread:\_
> \[New HTTP Request/Response Help\](https://forums.sketchup.com/t/new-http-request-response-help/36794)

… and referenced the similar standard library examples at:

> **[Class: Net::HTTP (Ruby 2.2.4)](https://ruby-doc.org/stdlib-2.2.4/libdoc/net/http/rdoc/Net/HTTP.html)**
>
> Class : Net::HTTP - Ruby 2.2.4

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 5, 2017, 6:08pm UTC](https://forums.sketchup.com/t/new-http-request-response-help/36794/4 "2017-01-05T18:08:52Z")

</div>

**(1)** @tt_su, 3 years ago, when the Ruby 2.0 Standard Library was distributed with SketchUp 2014, you had posted a warning that downloading large files with **`Net::HTTP`** blocked SketchUp (ie, could cause it to go into “Not Responding” mode.)

The docs are not clear, but I assume that this special API `Sketchup::Http` classes solve this main blocking issue ?

Ie, both classes overview sections are terse, and the `Sketchup::Http` module’s overview is the only place that “asynchronous” is mentioned, and that this module is “an alternative” to using **`Net::HTTP`**. But we will find often readers will not visit the page for a “namespace” module, and go right to the nested class documentation.

* * *

**(2)** At that same time Bugra had mentioned that connecting to https sites (with **`Net::HTTP`** ) crashed SketchUp due to issues with **`OpenSSL`**.

Again, documentation doesn’t mention whether these new API classes solve this **`OpenSSL`** issue.
