# Sending json post request in thirdparty api from sketchup

**URL:** <https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009>\
**Category:** Ruby API\
**Tags:** sketchup, json, http, post, rubyapi\
**Created:** [July 1, 2020, 9:01am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009 "2020-07-01T09:01:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arafathossain847](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@arafathossain847](https://forums.sketchup.com/u/arafathossain847)\
**Post date:** [July 1, 2020, 9:01am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/1 "2020-07-01T09:01:23Z")

</div>

I am trying to send a post request from sketchup to the api i made.I have web dialog.On the web dialog,On click the “save” button the post request will be executed.I want to send the information as json.I have been able to access the api i made.How can i access the length,width and send the length,width,volume from the sketchup model as json.here is the model i wrote:

```ruby
  def self.show_dialog
    @dialog ||= self.create_dialog
    @dialog.add_action_callback("ready") { |action_context|
      self.update_dialog
      nil
    }
    @dialog.add_action_callback("accept") { |action_context, value|
      self.update_material(value)
      @dialog.close
      nil
    }
    @dialog.add_action_callback("cancel") { |action_context, value|
      @dialog.close
      nil
    }
    @dialog.add_action_callback("save") { |action_context, value|
      self.update_material(value)
      request = Sketchup::Http::Request.new("http://127.0.0.1:5000/api/v1/projectStatus/save", Sketchup::Http::POST )
      request.start do |request, response|
        puts "body: #{response.body}"
      end
      nil
    }
    @dialog.show
  end

```

I want send the the post request something like this:

```
{
    "length": "11",
     "width": "12",
    "volume": "168"
}

```

---

<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:** [July 1, 2020, 10:14am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/2 "2020-07-01T10:14:44Z")

</div>

Please post code correctly on the forum (and make tabs are converted to spaces [2 for Ruby, 4 for other languages.)] _SEE_ …

- [[How to] Post correctly formatted and colorized code on the forum?](https://forums.sketchup.com/t/how-to-colourize-code-on-the-forum-wiki/46189)

---

<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:** [July 1, 2020, 10:27am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/3 "2020-07-01T10:27:16Z")

</div>

> [@arafathossain847](#):
>
> I want send the the post request something like this:
> 
> ```ruby
> {
> "length": "11",
> "width": "12",
> "volume": "168"
> }
> 
> ```

Well then you’ll either need to set that JSON as the request body or the request headers.

* * *

SketchUp’s `Http::Request` class takes a Ruby [hash](https://ruby-doc.org/core-2.5.5/Hash.html) to set headers. It is really dead simple to convert between several hashlike Ruby classes and JSON object strings using Ruby’s [**`JSON`**](https://ruby-doc.org/stdlib-2.5.5/libdoc/json/rdoc/index.html) library.

```ruby
json_text = '{
    "length": "11",
     "width": "12",
    "volume": "168"
}'
hash = JSON.parse(json_text)
req = Sketchup::Http::Request.new(
  'http://127.0.0.1:5000/api/v1/projectStatus/save', Sketchup::Http::POST
)
req.headers= hash
# ... etc. ...

```

* * *

Or … if using the body (same `json_text` string) …

```ruby
req = Sketchup::Http::Request.new(
  'http://127.0.0.1:5000/api/v1/projectStatus/save', Sketchup::Http::POST
)
req.headers= { 'Content-Type' => 'application/json' }
req.body = json_text
# ... etc. ...

```

---

<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:** [July 1, 2020, 10:42am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/4 "2020-07-01T10:42:10Z")

</div>

> [@arafathossain847](#):
>
> How can i access the length,width and send the length,width,volume from the sketchup model as json.

I’m not sure if you are asking where to get this information so I’ll give an answer for the model bounding box …

```ruby
model_properties = {}
model = Sketchup.active_model
bb = model.bounds
model_properties['height']= bb.height
model_properties['width']= bb.width
model_properties['depth']= bb.depth
model_properties['volume']= bb.height * bb.width * bb.depth
json_text = model_properties.to_json

```

_REF:_

- [`Geom::BoundingBox`](https://ruby.sketchup.com/Geom/BoundingBox.html) `class`
- [`Sketchup::Model#bounds`](https://ruby.sketchup.com/Sketchup/Model.html#bounds-instance_method) `instance method`

* * *

If you are interested in more precise total volume of actual components used, it will require iterating through the model and summing the volumes of manifold groups and components.

---

<div class="post-metadata">

**Author:** ![arafathossain847](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@arafathossain847](https://forums.sketchup.com/u/arafathossain847)\
**Post date:** [July 1, 2020, 10:44am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/5 "2020-07-01T10:44:38Z")

</div>

yes that what i was asking for.I am new to both skechup and ruby.Thats why couldnot explain it in a better 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:** [July 1, 2020, 10:45am UTC](https://forums.sketchup.com/t/sending-json-post-request-in-thirdparty-api-from-sketchup/130009/6 "2020-07-01T10:45:54Z")

</div>

I think most likely you’d send custom json (like this) **in the request body** , rather than mess with custom headers.
