Sending json post request in thirdparty api from sketchup

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:

  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"
}

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

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 to set headers. It is really dead simple to convert between several hashlike Ruby classes and JSON object strings using Ruby’s JSON library.

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) …

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. ...

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

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:


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.

1 Like

yes that what i was asking for.I am new to both skechup and ruby.Thats why couldnot explain it in a better way.

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