Ruby rest-client API request for Javascript Blob

Viewed 732

I am trying to create a rest-client request in Ruby for the API request that is triggered in this page. (source)

From looking at the Javascript in the page, I noticed that there is a Javascript Blob being created and the JSON content appended to that and then submitted in a multipart form with the following script -

I tried to emulate this with the rest-client gem in ruby with the following code -

namespace :materialize do
  task :connect => :environment do
    base_uri = "https://imatsandbox.materialise.net/web-api/cartitems/register"
    request = '{
     "cartItems":[
     {
       "toolID":"d65e1eca-7adf-453d-a3bb-eb051fffb567",
       "MyCartItemReference":"some reference",
       "modelID":"62352bab-d490-410c-851d-bc62e056e82a",
       "modelFileName":"",
       "fileUnits":"mm",
       "fileScaleFactor":"1",
       "materialID":"035f4772-da8a-400b-8be4-2dd344b28ddb",
       "finishID":"bba2bebb-8895-4049-aeb0-ab651cee2597",
       "quantity":"1",
       "xDimMm":"12",
       "yDimMm":"159.94",
       "zDimMm":"12",
       "volumeCm3":"2.0",
       "surfaceCm2":"100.0",
       "iMatAPIPrice": "25.0",
       "mySalesPrice": "26.0",
     }
     ],
     "currency":"EUR"
  }'
File.open('request', 'wb') do |f|
    f.write request
end


  response = RestClient.post base_uri, {:data => request, headers: {:multipart => true, accept: :json}}
  puts response.request
 end
end

The response body I always get -

"{\"error\":{\"message\":\"Wrong request body. Check if all parameters set correctly\",\"code\":401},\"cartItems\":[]}"

What am I doing wrong?

3 Answers
Related