Hi,
Am trying to get data from API url. In this API URL I have to pass payload also. When am trying to send request am getting below reply only.
{
"Data": ,
"Message": "No data Available",
"Status": "InputError"
}
When am using the same URL with payload it's working in Postman application.
Note:
In this API we are not using parameters to pass.
I would recommend using system.net.httpClient | Ignition User Manual
Example script at the bottom (replace get
with post
)
# Create the JythonHttpClient.
client = system.net.httpClient()
# Sent a GET request.
response = client.get("https://httpbin.org/get", params={"a": 1, "b": 2})
# Validate the response.
if response.good:
# Do something with the response
print response.json['args']['a']
Read through the whole documentation - note this part at the top
Be aware that httpClient instances are heavyweight, so they should be created sparingly and reused as much as possible. For ease of reuse, consider instantiating a new httpClient as a top-level variable in a project library script.
2 Likes
I would also pay attention to the type of http request you're making.
In your Script Console example, you're using system.net.httpPost
, but in Postman, you're using a GET
request. POST
and GET
are almost always routed differently in an API. GET
is used for reading data, and POST
is used for writing data (by convention).
Try using the system.net.httpGet
method instead. That might work for you.
2 Likes
@Brandon_Peterson Both are tried. Even it's not working.
There is a small difference is there in API parameter passing. Here am using payload / body parameters in API.
If using no payload API working fine. When am using payload parameter only am facing issue.
@bkarabinchak.psi Same problem. httpPost and httpClient both are showing same kind of problem only.
Your screenshot with the script console shows that the API is returning something. It's returning json with some infos in it, and particularly that you have an input error.
If the parameters look like the ones in postman, maybe there's a type issue somewhere, or some formatting error.
1 Like
Hi,
Below code is working fine. Here we are passing parameter in json format and getting json data.
'''
import json
from system.net import httpClient
url = "http://192.168.0.1/server/api/order_pending"
payload = {
"ORG_ID": "xx",
"DEPARTMENT": "yyyyy",
"RESOURCE": "zzzzz"
}
payload_json = json.dumps(payload)
client = httpClient()
response = client.post(url, data=payload_json)
print("Response Text:")
print(response.getText())
'''
Did you see what @Brandon_Peterson pointed out? You are doing a GET
request in your postman picture but you're doing a POST
in your script.
My guess is you need to do it with a GET
(and perhaps use params instead of data). Also, don't convert to json first. Read the docs I sent you -
The function itself encodes the dictionary to JSON. It expects a dictionary as one of the acceptable datatypes. If you give it JSON directly - that may be messing with things.
3 Likes