Hello everybody,
For some days now, I am facing a problem while making a request with POST data, and I have to admit I clearly do not understand what is going on. Let me explain in details.
For my project I have to use the API provided by i.materialise.
I am already using some of their API like “Materials Catalog”, “Upload Model”, “Pricing by Model ID”. Any problem for these.
Now I have to use the “Cart Item Creation API” but I cannot get it working on server side. I always get the error :
"error":{"message":"Wrong request body. Check if all parameters set correctly","code":401},"cartItems":[]}
I am not doing something complicated, just a simple Meteor.call()
from the client, and the method on the server is very simple :
On client
Meteor.startup(function () {
Meteor.call( 'imaterialise.cartItemCreate' );
});
On server
Meteor.methods({
'imaterialise.cartItemCreate': function imatCartItemCreate() {
var endpoint = 'https://imatsandbox.materialise.net/web-api/cartitems/register';
var dataToSend = {
"cartItems": [{
"toolID" : "<MY-TOOL-ID>",
"MyCartItemReference": "POKpojkdza",
"modelID" : "<MY-MODEL-ID>",
"fileUnits" : "mm",
"fileScaleFactor" : "1",
"materialID" : "6d65e2e0-6eee-4df4-951d-e5db38bd1660",
"finishID" : "2f01fc24-f2df-4e3e-abd3-a5408fe6f836",
"quantity" : "1",
"xDimMm" : "1",
"yDimMm" : "1",
"zDimMm" : "1",
"volumeCm3" : "0.11",
"surfaceCm2" : "1",
"iMatAPIPrice" : "0",
"mySalesPrice" : "0"
}],
"currency": "GBP"
};
HTTP.call( 'POST', endpoint, {
headers: {
'Accept': 'application/json',
// 'Content-Type' : 'multipart/form-data',
},
data: dataToSend,
}, function (error , response) {
console.log( 'imaterialise.cartItemCreate()', error, response );
});
},
});
Of course I tried to put the variable dataToSend
everywhere, in params
, in content
, in query
, stringified or not, …
However, what is really strange is that if I try to make this POST request with jQuery it works perfectly. (see this JSFiddle) (I replaced sensitive data like the toolID
and the modelID
, but trust me it works )
If I copy this code into a file on client-side (example client/main.js
) it works perfectly. (whereas I was expecting the opposite, as in general it is more recommended to make HTTP requests on server side, no?)
The data I send is correct (as we can see on the JSFiddle), the HTTP call is very simple, and even if the project I am currently working on uses a very old version of Meteor (1.1.0.2 …) I tried with a blank project with version 1.6.1 and I get the same error … I am completely lost.
Am I doing something wrong with this simple POST request ?
Do you think it could be related to the server providing the API ?
I hope I was clear enough and did not forget to mention any useful information, if not do not hesitate.
Thanks a lot !