2

Background

I have succesfully made some HTTP GET requests with pypiwin32 using

import pythoncom
import win32com.client

pythoncom.CoInitialize()

h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0) # log in automatically
h.Open('GET', url, True)
h.Send()

and later getting the response status and text from h.status and h.responseText

Problem

pywin32 (or should I say pypiwin32) does not seem to have official docs, and the Microsoft WinHttpRequest object docs has only C++ examples.

Question

How to make a HTTP POST request with a specific payload and Headers using the win32com.client from pywin32? Lets say that the HTTP Request Headers I want to add are

Referer: http://example.com/analysis.aspx?ID=527776455
Cookie: ASP.NET_SessionId=51jrf2r

and the payload I want to POST is

{"Id":"8974552","Action":"Analysis"}
Community
  • 1
  • 1
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • Wouldn't be requrests a better way? Because of cross-platform and great docs. – Attila Kis Feb 02 '18 at 08:18
  • 1
    I really need the automatic login provided by the `h.SetAutoLogonPolicy(0)` for Microsoft NTLM authentication. If that is possible with `requests` I would be happy to migrate. I have also tried [requests-ntml](https://github.com/requests/requests-ntlm), but it seems that you must provide the credentials if using that. – Niko Föhr Feb 02 '18 at 08:21

2 Answers2

1
import pythoncom
import win32com.client

pythoncom.CoInitialize()

h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0) # log in automatically

h.Open('POST', url, True)

h.SetRequestHeaders(Your_Headers)
h.Send("{"Id":"8974552","Action":"Analysis"}")
Attila Kis
  • 521
  • 2
  • 13
0

After some trials and errors, I think I got it right. Here is an example using httpbin. I found out that using the json.dumps() is quite handy since it automatically writes False as 'false' and None as 'null'.

import json
import win32com.client

h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.Open('POST', 'http://httpbin.org/post', True)

h.SetRequestHeader('Referer', 'http://example.com/analysis.aspx?ID=527776455')
h.SetRequestHeader('Cookie', 'ASP.NET_SessionId=51jrf2r')

payload = dict(
    Id = 8974552,
    Action = "Analysis",
    somebool = False,
    missing_parameter = None
)

h.Send(json.dumps(payload))

print(h.responseText)

Here is the output of the print command, with IP address removed:

{
  "args": {},
  "data": "{\"Id\": 8974552, \"Action\": \"Analysis\", \"somebool\": false, \"missing_parameter\": null}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Connection": "close",
    "Content-Length": "83",
    "Content-Type": "text/plain; Charset=UTF-8",
    "Cookie": "ASP.NET_SessionId=51jrf2r",
    "Host": "httpbin.org",
    "Referer": "http://example.com/analysis.aspx?ID=527776455",
    "User-Agent": "Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)"
  },
  "json": {
    "Action": "Analysis",
    "Id": 8974552,
    "missing_parameter": null,
    "somebool": false
  },
  "origin": "###.###.###.###",
  "url": "http://httpbin.org/post"
}
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96