03/08/2015

[Python] HTTP POST

Here is a sample piece of code on how to issue HTTP POST requests with an XML payload from Python


 URI = 'https://httpbin.org/post'  
 PARAMETERS="<NODE>VALUE</NODE>"  
   
 from System.Net import WebRequest  
 from System.Text import Encoding  
   
 request = WebRequest.Create(URI)  
 request.ContentType = "text/xml"  
 request.Method = "POST"  
   
 bytes = Encoding.ASCII.GetBytes(PARAMETERS)  
 request.ContentLength = bytes.Length  
 reqStream = request.GetRequestStream()  
 reqStream.Write(bytes, 0, bytes.Length)  
 reqStream.Close()  
   
 response = request.GetResponse()  
 from System.IO import StreamReader  
 result = StreamReader(response.GetResponseStream()).ReadToEnd()  
 print result  

No comments:

Post a Comment

With great power comes great responsibility