Last active
February 12, 2023 00:11
-
-
Save surajkjai/1ab76fb3a6aacd2be332b94362ae8de1 to your computer and use it in GitHub Desktop.
Use pycurl to POST http data to a REST endpoint over TLS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pycurl | |
def pycurl_post_over_tls(): | |
with open('data_to_read.json', 'r') as f: | |
url = "https://httpbin.org/post" | |
# load json data from the file read | |
json_data = json.load(f) | |
# stringify the json data (now json_data is dict) to be used in the POST body | |
# (pycurl will crap if directly fed the dict type in POST) | |
post_data = json.dumps(json_data) | |
# headers if any | |
# use the append method to add headers | |
# e.g. headers.append('FOO: {}'.format('Bar')) | |
headers = [] | |
c = pycurl.Curl() | |
c.setopt(pycurl.POST, 1) | |
c.setopt(pycurl.POSTFIELDS, post_data) | |
# set SSL_VERIFYPEER to 1 if you want to verify the server certificate | |
# checkout pycurl.CAINFO to setup your certchain | |
# e.g. c.setopt(pycurl.CAINFO, '/etc/ssl/certs/') | |
c.setopt(pycurl.SSL_VERIFYPEER, 0) | |
c.setopt(pycurl.URL, url) | |
c.setopt(pycurl.HTTPHEADER, headers) | |
c.perform() | |
if __name__ == '__main__': | |
pycurl_post_over_tls() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment