cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a model for SAP AIN in a SAPUI5 Dashboard using the API?

Former Member

I tried to create a model for SAP AIN in a SAPUI5 Dashboard using the API. I got the response "HTTP 415 Unsupported Media Type".

var payload = {
    "internalId": sModelId,
    "templateId": "<ID>",
    "organizationID": "<ID>",
    "description": {"short": sModelId},
    "equipmentTracking": "1"
};
oJson.loadData("/ain/services/api/v1/models", encodeURI(JSON.stringify(payload)), true, "POST");

oJson is a JSONMOdel. "/ain/" provides the destination to the API. How can I deliver the payload correctly. What is my mistake?


2nd question:

I did successfully use a GET request targetting "/ain/services/api/v1/models" to list all the models. But when I try to set a filter inside the query parameters, the filter won't work when there is a '+' inside the filtered manufacturer string. I did use uri encoding.

This is my code:

var sQuery = "$filter=(manufacturer eq '" + encodeURIComponent(sManu) + "')";
this._oFilteredModel.loadData("/ain/services/api/v1/models", sQuery);

sManu is a string defining the manufacturer filtered by and _oFilteredModel is a JSONModel.

Accepted Solutions (1)

Accepted Solutions (1)

ingo_neumann2
Employee
Employee

Hi Niclas,

Issue 1: In your snippet, you are using oParameters (second parameter) from the loadData function to attach your uri-encoded payload to the request. According to the SAPUI5 documentation, these parameters are handled as URL parameters.

SAP UI5 API Reference: oParameters -> "A map or string that is sent to the server with the request. Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is an object (map), it is converted to a string and url-encoded before it is appended to the URL."

In your particular case it results in the request being handled as Content-Type "application/x-www-form-urlencoded" automatically. This will furthermore result in the 415 response.

In the API Documentation for SAP Asset Intelligence Network you will see, that the Content Type "application/json" is required. In addition to that, the request JSON payload must be always put into the body of the POST request.

Mentioning that, my recommendation would be to use a more flexible approach to get your data (AJAX Request, XHR, ...)
Please try the following snippet, which should work fine.

			var payload = {
			    internalId: sModelId,
			    templateId: "<ID>",
			    organizationID: "<ID>",
			    description: {
			    	short: sModelId
			    },
			    equipmentTracking: "1"
			};
			
			
			jQuery.ajax({
				type: "POST",
				contentType: "application/json",
				url: "/ain/services/api/v1/models",
				data: JSON.stringify(payload),
				success: function(data, textStatus, jqXHR) {
					oJson.setData(data);
			}
			});


Issue 2:

I could replicate your issues both in SAPUI5 and with Postman.
Encoding your filter value is definitely the right way to go, but it seems, that there is an issue with "+" signs in the filter query (both encoded and unencoded)

I tried the exact same thing with the encoded "&" sign and the correct objects are returned.

Please create a support ticket to get this potential bug checked by the development teams.


Best Regards

Ingo

Former Member

Hi Ingo,

Thank you very much! I tried using ajax as you recommended and it works. For Issue 2 I will create a support ticket.

Best Regards
Niclas

Answers (0)