[SOLVED] Does anyone know how to make a WebRequest in Python/Ignition?

Good morning,

I am converting the following code:

myRequest = (HttpWebRequest)WebRequest.Create(this.URL); // create request
myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(this.BasicAuthAccount + ":" + this.BasicAuthPassword)));
myRequest.Method = Method.ToString();
myRequest.KeepAlive = false;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ReadWriteTimeout = 1000;

if (this.HasData)//Append data for send
{
    byte[] byData = Encoding.ASCII.GetBytes(this.JsonifyString); // convert POST data to bytes
    myRequest.ContentLength = byData.Length;
    // Add the post data to the web request
    outputStream = myRequest.GetRequestStream();
    outputStream.Write(byData, 0, byData.Length);
    outputStream.Close();
}

I think I use this Ignition method:
https://docs.inductiveautomation.com/display/DOC81/system.net.httpPut

And I came up with the following code:

url = "http://192.168.1.7/do_value/slot_0/ch_0"
contentType = "application/x-www-form-urlencoded"
putData = {"Ch":0, "Val": 0}
connectTimeout = 1000
readTimeout = 1000
username = "root"
password = "00000000"
 
system.net.httpPut(url, contentType, putData, connectTimeout, readTimeout, username, password)

And I packaged this code into an Ignition button script and am getting the following error:

Traceback (most recent call last):
  File "<event:actionPerformed>", line 10, in <module>
IOError: Unexpected end of file from server

Does anyone know what this error means?

You’re better off using system.net.httpClient - Ignition User Manual 8.1 - Ignition Documentation and then from there using the .put() method. Try it with httpClient first and see if the issue persists.

I think you’re trying to use the POST method, not PUT (based on comments in what you’re trying to replicate), so that could be part of the problem as well.

I am getting a response 411 length required.

Try sending the JSON as data, not params. Right now you are calling PUT with an empty request body.

1 Like

That was the key… thank you Kevin and others.