‎2017 Sep 20 10:19 PM - last edited on ‎2024 Feb 04 12:20 AM by postmig_api_4
Could anyone please provide example API usage with Python? Currently the "Generate Code" option displays examples in Javascript, Java, Swift, Curl, ABAP and SAPUI5. For example this answer describes using prodimgclassifier and I would like to know ways to call other APIs such as time-series prediction, language translation etc.
Thanks.
‎2017 Oct 06 8:11 PM
The link you provided makes this out to be a little more complicated than it needs to be. At it's core with any REST service, you need the url you want to retrieve, the headers you'd like to send along with it and in the case of a POST the data you want to send.
Here's an example for the Time Series Forecast API you mentioned. Assuming you have the requests library installed (if not, install it) all you have to do is specify the url, create the headers and data as a dict, and allow the requests library to take care of the post action for you.
import requests
import json
headers = {
'APIKey' : 'your API Key',
'Accept' : 'application/json',
'content-type' : 'multipart/form-data'
}
data = {
'options' : 'string'
}
url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"
resp = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)
‎2017 Oct 06 8:11 PM
The link you provided makes this out to be a little more complicated than it needs to be. At it's core with any REST service, you need the url you want to retrieve, the headers you'd like to send along with it and in the case of a POST the data you want to send.
Here's an example for the Time Series Forecast API you mentioned. Assuming you have the requests library installed (if not, install it) all you have to do is specify the url, create the headers and data as a dict, and allow the requests library to take care of the post action for you.
import requests
import json
headers = {
'APIKey' : 'your API Key',
'Accept' : 'application/json',
'content-type' : 'multipart/form-data'
}
data = {
'options' : 'string'
}
url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"
resp = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)
‎2018 Jul 19 9:32 AM
I have tried your snippet and modified it accordingly. PFB the snippet
import requests
import json
headers = { 'APIKey' : 'xyz', 'Accept' : 'application/json', 'content-type' : 'application/json' }
data = { 'options' : {"period":"12"},'texts':['1,2,3,4,5,6,7,8,9,10,11,12'] }
url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)
Output:
{ "_id": "adb69b92-8f38-463c-aff4-54bae08a5637", "error": "Invalid request", "error_description": "This service requires at least 1 file or 1 text Please put your input into the \"files\" or \"text\" field of the POST request", "processed_time": "Thu, 19 Jul 2018 08:31:59 GMT", "request": { "files": null, "options": {}, "tenantName": "imgclassif-tech-user", "texts": [] }, "status": "FAILED", "status_code": 400, "tenantName": "imgclassif-tech-user" }
Please Help!!!!!
‎2018 Jul 19 4:27 PM
Your request is being made with a `texts` parameter but the error message seems to suggest you should be sending `text`