1

I'm porting some automation scripts from bash to python, and they're almost ll curl commands of the following format:

curl -k -H "Content-Type: application/json" -X POST -d '{               "Request": {
                          "MessageID": "xxxx",
                          "MessageDateTime": "xxxxx",
                          "SourceSystem": "xxxx",

           }
}' https://myUrl.xxx

What's the best way to structure this in Python accurately? So far I have:

import requests

headers = {'Content-Type': 'application/json'}
payload = {'All the data'}
conn = httplib.HTTPConnection("myUrl.xxx")
conn.request("POST", "", payload, headers)
response = conn.getresponse()
print response

I want to make sure the -k, -d, and -x bash options are being reflected in this script. Thank you!

1
  • You import requests but don't use it. Commented Jun 9, 2016 at 16:24

1 Answer 1

2

You can use requests.post directly. -k corresponds to verify=False:

from datetime import datetime as DateTime
import requests
import json

URL = "https://myUrl.xxx"

message = {
    "Request": {
        "MessageID": "xxxx",
        "MessageDateTime": DateTime.now().isoformat(),
        "SourceSystem": "xxxx",
    }
}

response = requests.post(URL, data=json.dumps(message), verify=False, headers={"Content-Type":"application/json"})
data = response.json()
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, thank you. I'm new to Python, so in the message key pair structure, how can I insert a variable containing the current ISO data timestamp after "MessageDateTime"? Thank you!
@EschersEnigma import datetime; now = datetime.datetime.now() ... "MessageDateTime": now
You can also say json=message, which does the json.dumps and the headers for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.