Issue when sending JSON raw data in request body using Sketchup::Http module

I’m using the Trimble Connect API to import/export objects to/from SketchUp, but recently ran into some issue when calling HTTP requests with the SketchUp::Http module. All the other calls I’ve tried so far work fine, but there is one where I have to send the request body as json raw and I can’t make it work. Tried with Postman and it works fine, so the configuration is ok. See below the request documentation and my code:

tc_token = MY_TOKEN
connect_host = 'app.connect.trimble.com/tc/api/2.0'
tag_id = MY_TAG_ID
object_id = MY_OBJECT_ID

url = 'https://' + connect_host + '/tags/' + tag_id + '/objects'
header = { Authorization: 'Bearer ' + tc_token, 'Content-Type' => 'application/json' }
payload = [{ id: object_id, objectType: 'FILE' }].to_json

request = Sketchup::Http::Request.new(url, Sketchup::Http::DELETE)
request.headers = header
request.body = payload

request.start do |req, res|
  status = res.status_code

  if status == 204
    # Done correctly
  else
    # Some error
  end
end

I’ve also tried with:

payload = "[\n\t{\n\t\"id\": \"#{object_id}\", \"objectType\": \"FILE\"\n}\n]"

but it does not work either. I always get a Bad Request (Error 400) in the response.

Try …

header = {
  'Authorization' => "Bearer #{tc_token}", 'Content-Type' => 'application/json' 
}

Ie you had "Authorization:" instead of "'Authorization' =>"

Try to use a tool like PostMan to make the request, see if that works.

Then also use a tool like Fiddler to capture the HTTP traffic and compare the requests for their differences:

If you cannot get any requests to work you should reach out to Trimble Connect support and see if you’ve run into issues with their API or documentation.

@DanRathbun That doesn’t work either, I have another requests with that same convention and they work, the issue is in the body. I’ve tried many configurations, but the response is always the same.

@tt_su I have tried with Postman (in fact I mentioned it in the post), and it works. From Ruby I’m getting a Message Exception, so the body is wrong. I also contacted with TC but they cannot help me with the Ruby piece. See below the Postman captures to compare with the code. The call (header and body) is the same, but it’s working in Postman (Status 204) and not with Sketchup::Http (Status 400 and MessageException).

I am going to try with Net::Http too.

Have you used Fiddler to verify the HTTP request being sent from your Extension?

All examples use only a hash converted .to_json without the surrounding array …
(I also use the URI library myself when possible.)

Also, I have noticed and posted that the SketchUp Http classes do NOT work the same as Net::HTTP classes with respect to capitalization of header fields and names of methods.

Using exact matching capitalization is especially important when testing response headers.

I posted the differences that I found here

Found a solution yet ?

@DanRathbun Not yet… I also tried with Net/Http with the following configuration:

uri = URI.parse('https://' + connect_host + '/tags/' + tag_id + '/objects')
headers = { 'Authorization' => "Bearer #{tc_token}", 'Content-Type' => 'application/json' }
http = Net::HTTP.new(uri.host, uri.port)

req = Net::HTTP::Delete.new(uri.path, headers)
# req = Net::HTTP::Delete.new(uri, headers)
req.body = [{ id: object_id, objectType: 'FILE' }].to_json
res = http.request(req)
body = res.body

But the code is crashing in the res = http.request(req) line, so there is something wrong too… The surrounding array is the thing that annoys me most, but it has to be included in the body to get the right response, as you can see in the capture of my previous post. See below a failed test without the [,] brackets.

@tt_su I used Fiddler but I can only see the traffic whose response is HTTP 200. The HTTP 204 or HTTP 400 responses don’t show up, neither from Postman nor from the extension. Other requests that I’m calling and are correct are showing up, see below.


image

Have you enabled a filter to display only 200 responses?