HTTParty and JSS API

skoonin
New Contributor

I've been working on a script to either delete computers or unassign the users from them using the JSS API in ruby using httparty.

I've so far been able to make GET commands work for computer info, but I can't seem to find any info on the syntax of a PUT command (using computer serial number).

Anyone out there have any luck getting this to work and looking to share?

Thanks!

2 REPLIES 2

ChrisL
New Contributor III

Hi @skoonin,

I just saw this post now, a month later.

I'm not familiar with httparty, but I'm very familiar with using the API via ruby, and have shared my work here: http://pixaranimationstudios.github.io/ruby-jss/index.html

ruby-jss uses the rest-client gem under the hood, but abstracts all of the GETting and PUTting away from the user.

Using it to delete a computer (after requiring ruby-jss and connecting to the API) would look like this:

JSS::Computer.new(id: 1234).delete

and to clear out the username:

computer = JSS::Computer.new(serialnumber: 1234)
computer.username = nil
computer.save

or to clear out all the user & location data:

computer = JSS::Computer.new(serialnumber: 1234)
computer.clear_location
computer.save

Under the hood, it's building the appropriate XML and passing it in the body of a PUT request to https://server.address.org:8443/JSSResource/computers/serialnumber/1234

(well, it always use the jss id, not the serial number, for API calls once it knows the id,but the above URL works the same)

A quick look at the httparty github repo makes me think you can do something like :

# set the headers as needed... 
options = {
  body: '<your xml here>',
  headers: {'Accept' => 'application/json'}
}

HttpartyObject.post('/JSSResource/computers/serialnumber/1234', options)

hopefully all that's helpful. Feel free to reach out if you have more questions.
Cheers,
-Chris

skoonin
New Contributor

Hey Chris -

Thanks for the response, and I have looked at your JSS gem before and it's super cool. I chose not to use it because I was teaching myself ruby and API stuff. I did managed to get it to work with HTTParty finally.

You're syntax for HTTParty is totally right up there. I ended up using (as an example):

HTTParty.put("#{jss_base_url}/computers/serialnumber/<serialnumber>", 
                                body: "<XML>", 
                                headers: { 'Content-Type' => 'text/xml' }, 
                                verify: false, basic_auth: <auth info> )

Now that I understand it, I can look into using your gem in the future if I need it. Thanks again for the reply.

shawn