I recently tried to send a request with an image using the sketchup api, but I couldn’t find the information about how to set the body.My body information and headers information are as follows:
Well firstly, File.open doesn’t do anything but open the file and return a reference to the file object. In other words it does not actually read the file data.
Secondly, raster image files are binary format so you’d probably need to pass a "rb" (read binary) mode parameter to the File method that is used.
Also, keep in mind that the File class is a subclass of IO and inherits many methods for it’s superclass. You could instead just use IO::binread which ensures that the file is closed after the read.
begin
image_data = IO.binread(image_path)
rescue => err
# handle the error and bailout
else
@request.body= image_data
end
Note that it is best practice to wrap any file IO operations within a begin … rescue … end block.
If you are going to use multipart body, you’ll need to insert a boundary and define that boundary (I think in the header.)
The Ruby library Net::Http requests are blocking. This is why SketchUp’s API implemented asynchronous requests.
The Ruby request classes are similar to the File class in that the basic functionality is inherited from the Net::HTTPGenericRequest superclass (ie, this is where the #body= setter is defined.)
If the response content type is also "application/json" then parse the response body back into a Ruby hash …
@request.start do |request, response|
headers = response.headers
type = headers['Content-Type']
if type == 'application/json'
@data = JSON.parse(response.body)
else
@data = response.body
end
end
You can have other elsif clause(s) for other content types, of course.
BEWARE: The SketchUp API Http class headers are not case insensitive like the Ruby library classes are. So you must access the API header hashes with exact (correct) case key names.
Hi, I am having trouble understanding what you are trying to do.
Are you trying to use an external Rest API to download an image?
Below is an example of how I get a response from Rest API
require 'net/http'
require 'uri'
url = "#{HERE IS WHERE REST API URL GOES}"
uri = URI.parse(url)
request = Net::HTTP::Post.new(uri)
req_options = {
use_ssl: uri.scheme == 'https'
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
hash_response = eval(response.body)
json = JSON.parse(hash_response)
# Below is an example if json API has key value of "image"
json["image"]
Again not sure if this is what you are looking for.