API call with script getting IOError: no protocol

Hi I am new to Ignition and trying to learn quickly. I am currently trying to figure out the formatting needed to post API calls with rest via script. Currently I am getting an error when I execute the script and I can’t quite figure out what I have incorrect. Below is my script being used. Followed by the error I am receiving. Any ideas on what I have incorrect?

import pprint

pp = pprint.PrettyPrinter(indent=4)
headers = {
#Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': 'MYSubscriptionKEY',
}
body = {

    "resourceGroup": "Tugger",
    "location": "B05",
    "description": "Test with Python",
    "requestType": "Pull",
    "itemNo": "18308-TGZ-A030-21",
    "containerCount": 1 
}

#Get the json response from Cella
url = "https://api.cella-tech.com/RequestManagement/v1/ResourceGroup/Request", body, headers
CellaResponse = system.net.httpPost(url)
CellaJSON = system.util.jsonDecode(CellaResponse)

#print to see the response
print pp.pprint(CellaJSON)
Traceback (most recent call last):
  File "input", line 21, in module
IOError: no protocol: ('https://api.cella-tech.com/RequestManagement/v1/ResourceGroup/Request', {'resourceGroup': 'Tugger', 'requestType': 'Pull', 'description': 'Test with Python', 'itemNo': '18308-TGZ-A030-21', 'location': 'B05', 'containerCount': 1}, {'Ocp-Apim-Subscription-Key': 'MYSubscriptionKEY', 'Content-Type': 'application/json'})

Can you fix your post so that the code is posted with proper formatting?

Either enclose it in triple backticks or use the preformatted text editor control.

Hey @Kevin.Herron I have updated this now.

I'm not sure what you think this line is doing, but it's probably not what you think.

This turns the url variable into a tuple, which is not the argument expected by httpPost.

Yea I have updated since then. Still getting a 400 error, I was just getting python down a little so the changes with the jython are getting me. Below is my current that I get a 400 return response. Below that is my python that works correctly though. I am not sure what is sending differently that it doesn’t like the post.

import pprint
pp = pprint.PrettyPrinter(indent=4)

headerValues = {
# Request headers

    'Content-Type': 'application/json',

    'Ocp-Apim-Subscription-Key': 'MySubscriptionKey',

} 

postData = "{'resourceGroup': 'Tugger','location': 'B05','description': 'Test with Python','requestType': 'Pull','itemNo': '18308-TGZ-A030-21','containerCount': 1 }"

# get the json response from Cella
url = "https://api.cella-tech.com/RequestManagement/v1/ResourceGroup/Request"

#CellaResponse = system.net.httpPost(url, "application/json", postData, 10000, 60000, "", "", headerValues)
CellaResponse = system.net.httpPost(url, "application/json", postData, 10000, 60000, "", "", headerValues, "false")

CellaJSON = system.util.jsonDecode(CellaResponse)

#print to see the response
print pp.pprint(CellaJSON)
import http.client
import json

connection = http.client.HTTPSConnection('api.cella-tech.com', 443, timeout=30)

headers = {'Content-type': 'application/json','Cache-Control': 'no-cache','Ocp-Apim-Subscription-Key': 'MySubscriptionKey'}

data = {

    "resourceGroup": "Tugger",

    "location": "B05",

    "description": "Test with Python",

    "requestType": "Pull",

    "itemNo": "18308-TGZ-A030-21",

    "containerCount": 1 

}

 

json_data = json.dumps(data)

connection.request('POST', '/RequestManagement/v1/ResourceGroup/Request', json_data, headers)

response = connection.getresponse()

print(response.read().decode())

I'm not sure JSON allows single quotes to be used...

You can put all this into a dictionary like you had before and then use system.util.jsonEncode to encode it as JSON.

Thank you for the suggestion, sorry if I sound ignorant on this. Is the below how that would be done properly?

Data = "{'resourceGroup': 'Tugger','location': 'B05','description': 'Test with Python','requestType': 'Pull','itemNo': '18308-TGZ-A030-21','containerCount': 1 }"

postData = system.util.jsonEncode(Data)

No, it would be like the body in your first post.

data = {
    "resourceGroup": "Tugger",
    "location": "B05",
    "description": "Test with Python",
    "requestType": "Pull",
    "itemNo": "18308-TGZ-A030-21",
    "containerCount": 1 
}

postData = system.util.jsonEncode(data)

@Kevin.Herron Thank you! I think that was it, it posted successfully! I am sure I will have more questions along the way but this was huge for me. Thanks for pointing me the right way.

You don’t specify your Ignition version, but most of 8.X and all of 8.1.X can use system.net.httpClient, which will take care of JSON encoding (of your outbound data) and decoding (of the inbound data) for you, e.g:

client = system.net.httpClient()

headers = {
    'Content-type': 'application/json',
    'Cache-Control': 'no-cache',
    'Ocp-Apim-Subscription-Key': 'MySubscriptionKey'
}

data = {
    "resourceGroup": "Tugger",
    "location": "B05",
    "description": "Test with Python",
    "requestType": "Pull",
    "itemNo": "18308-TGZ-A030-21",
    "containerCount": 1 
}

response = client.post('api.cella-tech.com' + '/RequestManagement/v1/ResourceGroup/Request', headers=headers, data=data)

if response.good:
    print response.json
1 Like

Thanks @PGriffith I will keep that in mind. So far I have downloaded the current version which looks like 8.1.15. Once I am able to make/run some POC’s I will move on from running it on my laptop in demo mode. I will definitely keep this in mind during my testing. I was able to use your script above but had to add https:// to the start of the url or it gave an error of no protocol.