Ended: October 15 Webinar: Get More Done with BigFix – Your Next Level BigFix REST API Questions Answered in Real Time

The Get More Done with BigFix Webinar series will give you exclusive access to our Team BigFix experts to learn more about BigFix and answer your questions in real time.

Rhonda Studnick Kaiser, Director of Client Experience, and John Talbert, Director of BigFix Professional Services, are your host and moderators.

In this edition, Michael Schwarz and Marjan Radanovic of our BigFix Professional Services team will be answering your questions about getting started with the BigFix REST API.

Register here: https://attendee.gotowebinar.com/register/817452119891795725

2 Likes

Hi, folks!

You watched my almost successful presentation on putting things in BigFix with the REST API. Sometimes demos go a little sideways, but in this case it may benefit your use of the APIs in the end. The problem was a simple, one-character problem. In Python β€˜β€™β€™ marks the beginning of a multi-line string constant that ends with another β€˜β€™β€™.

That was my problem. There should have been a backslash after the open β€˜β€™β€™ to prevent a newline being at the start of the string! BigFix doesn’t care about newlines anywhere else, but it expects the <?xml tag to be the very first thing it sees, or it is a malformed message. I’m not even sure when I deleted the backslash. Because I ran my demo the night before.

So, don’t let this bite you!

But I will share annotated python source THAT WORKS here and now. It contains a lot of comments explaining what went awry in the demo and offers more than one way to fix it.

Please do play with this code and feel free to ask me questions about it.

3 Likes
import requests
import json

# This is here ONLY to suppress self-signed certificate warnings
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# End of warning supression

bf_server = "10.10.220.247:52311"
bf_username = "HCLAdmin"
bf_password = "BigFix!123"

#sourcedAction = '''
#<?xml version="1.0" encoding="UTF-8" ?>
# For those qho attended the webinar, the lines above were what was broken in
# the demo code. When you look below you will note a backslash at the end of the
# triple quote. That prevents a newline from being included in the string. That
# newline was the problem! I literally could not see that I managed to delete the
# backslash at some point. This would also work:
#
#sourcedAction = '''<?xml version="1.0" encoding="UTF-8" ?>
#
# The point is, BigFix wants to see the XML tag **first** and nothing else.
# So here is the code, with explanation! It works properly.

sourcedAction = '''\
<?xml version="1.0" encoding="UTF-8" ?>
<BES xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<SourcedFixletAction>
	<SourceFixlet>
		<Sitename>Enterprise Security</Sitename>
		<FixletID>48001</FixletID>
		<Action>Action1</Action>
	</SourceFixlet>
	<Target>
		<ComputerName>BF2LABROOT</ComputerName>
	</Target>
	<Settings>
	</Settings>
	<Title>Programmatic Action</Title>
</SourcedFixletAction>
</BES>
'''

session = requests.Session()
session.auth = (bf_username, bf_password)
response = session.get("https://" + bf_server + "/api/login", verify=False)

qheader = {
	'Content-Type' : 'application/x-www-form-urlencoded'
}

req = requests.Request('POST'
	, "https://" + bf_server + "/api/actions"
	, headers=qheader
	, data=sourcedAction
)

prepped = session.prepare_request(req)
	
result = session.send(prepped, verify = False)

if (result.status_code == 200):
	print(result.text)  # Again, for webinar attendees:
	# The line above will print out the result on success. Here is what that might look like:
	# <?xml version="1.0" encoding="UTF-8"?>
	#<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
    #    <Action Resource="https://10.10.220.247:52311/api/action/64" LastModified="Thu, 15 Oct 2020 18:19:39 +0000">
    #            <Name>Programmatic Action</Name>
    #            <ID>64</ID>
    #    </Action>
	#</BESAPI>
	#
	# The ID is the action ID, which you could use in a GET url for the status, for example:
	#
	# https://10.10.220.247:52311/api/action/64/status
	#
	# Here's what that looks like:
	#<BESAPI xsi:noNamespaceSchemaLocation="BESAPI.xsd">
	#<ActionResults Resource="https://10.10.220.247:52311/api/action/64/status">
	#	<ActionID>64</ActionID>
	#	<Status>Open</Status>
	#	<DateIssued>Thu, 15 Oct 2020 18:19:40 +0000</DateIssued>
	#	<Computer ID="11394379" Name="BF2LABROOT">
	#		<Status>The action failed.</Status>
	#		<State IsError="0">4</State>
	#		<ExitCode>3010</ExitCode>
	#		<ApplyCount>1</ApplyCount>
	#		<RetryCount>1</RetryCount>
	#		<LineNumber>4</LineNumber>
	#		<StartTime>Thu, 15 Oct 2020 18:20:03 +0000</StartTime>
	#		<EndTime>Thu, 15 Oct 2020 18:20:53 +0000</EndTime>
	#	</Computer>
	#</ActionResults>
	#</BESAPI>
else:
	print("Fixlet POST failed.")
	print(result)
3 Likes

Everything following a hash mark above is a comment. I explain the demo issues in those comments and also the one bit of code we did not get to. You should feel free to remove those comments from the code when you use it.

3 Likes

Regardless of the sideways trip, it was still a good webinar and I really did learn a few things, so good job!

–Mark

1 Like

I know it was a good while ago but I found it very useful for me. Thanks for having it out there!

1 Like