Request Ruby Client API for Javascript Blob

I am trying to create a rest-client request in Ruby for an API request that runs on this page . ( source )

From a look at the Javascript on the page, I noticed that Javascript Blob is being created and the JSON content is added to it and then sent in a multi-page form with the following script -

I tried to imitate this with the rest of the client's stone 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 body of the answer that I always get is

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

What am I doing wrong?

+5
source share
3 answers

You get 401, which means your request is not allowed. I think you need to pass the credentials with your request. Check the note at the bottom of the page about transferring the registered email address to the api demo version: https://imatsandbox.materialise.net/api/demo/

It looks like you need to do this:

 https://i.materialise.com/web-api/materials?user=<your registered email address here> 
+2
source

To send mixed json and blob data you need to use mulpipart.

RestClient already has a RestClient implementation

And your solution will look like this:

 require 'rest-client' url = 'https://imatsandbox.materialise.net/web-api/cartitems/register' json = '{ "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" }' def stringfile(string, filename="file_#{rand 100000}", type=MIME::Types.type_for("json").first.content_type) file = StringIO.new(string) file.instance_variable_set(:@path, filename) def file.path @path end file.instance_variable_set(:@type, type) def file.content_type @type end return file end response = RestClient.post url, data: stringfile(json), file: [ File.new("./1.png", 'rb') ] puts response.body 

Result of response.body :

 { "currency": "EUR", "cartItems": [ { "myCartItemReference": "some reference", "cartItemID": "97884fef-d2ae-45e4-a18c-b52b3dcdcb9d", "toolID": "d65e1eca-7adf-453d-a3bb-eb051fffb567", "modelID": "c65278da-8693-4f49-a5c0-8be55a3e63b2", "modelFileName": "The_Club_7plus.obj", "fileUnits": "mm", "fileScaleFactor": 1.0, "materialID": "035f4772-da8a-400b-8be4-2dd344b28ddb", "materialName": "Polyamide", "finishID": "bba2bebb-8895-4049-aeb0-ab651cee2597", "finishName": "Natural-White-Polyamide", "quantity": 1, "xDimMm": 81.2660000000, "yDimMm": 159.9350000000, "zDimMm": 10.0960000000, "volumeCm3": 15.5864000000, "surfaceCm2": 260.2880000000, "iMatAPIPrice": 25.0, "mySalesPrice": 26.0, "mySalesUnitPrice": 26.0, "iMatPrice": 13.61, "validUntil": "2017-09-04T00:00:00+02:00" } ] } 

Hope this helps you;)

+2
source

I also sometimes get 401 when I use RestClient.post, I solve with Resource.new

 res = RestClient::Resource.new("http://www.sample.com/some.json",:headers => {'Content-Type' => "application/json"}) res.post(json_data) 
0
source

Source: https://habr.com/ru/post/1270755/


All Articles