Struggling to 'put' a hash using json or xml

482 views
Skip to first unread message

Jeff

unread,
Oct 21, 2011, 3:56:20 PM10/21/11
to HTTParty Gem

Hello all,

I'm hoping someone can point me in the right direction to solve a
challenge I'm having. I'm pretty new to Ruby, so please be patient
with me if I'm (probably) missing something really obvious…

Basically I'm have a hash that I need to submit to a REST webservice
using the 'put' command.

I'm able to "get" information out of the webservice pretty easily so
I'm pretty confident my authentication is good. Also, in a separate
script using bash I'm able to generate a separate XML file and use
curl to submit that successfully.

My hash is named comp, and looks like the following (viewed through
pp):

{"location"=>
{"building"=>"POC",
"position"=>nil,
"real_name"=>"Users Real Name",
"username"=>"account_name",
"phone"=>nil,
"email_address"=>nil,
"room"=>nil,
"department"=>"Profs"},
"general"=>
{"serial_number"=>"ComputerSerialNumber",
"name"=>"ComputerName",
"jamf_version"=>"8.22",
"distribution_point"=>"ServerName",
"last_contact_time"=>"2011-10-21 11:18:08",
"asset_tag"=>nil,
"report_date"=>"2011-10-21 11:25:00",
"alt_mac_address"=>"MAC_Address",
"id"=>"3919",
"sus"=>"Server_Name",
"mac_address"=>"MAC_Address",
"remote_management"=>
{"managed"=>"true",
"management_password"=>"********************",
"management_username"=>"account_name"},
"barcode_1"=>nil,
"netboot_server"=>"Server_name",
"barcode_2"=>nil,
"platform"=>"Mac",
"ip_address"=>"IP_Address"}}

------------------------

My current code:

#!/usr/bin/ruby

require 'rubygems'
require 'httparty'
require 'json'

class JSS
include HTTParty
format :xml
basic_auth 'user', 'password'
base_uri 'https://ServerDNSName:8443/JSSResource'
end

class JSS_Submit
include HTTParty
debug_output $stdout
format :json
basic_auth 'user', 'password
base_uri 'https://ServerDNSName:8443/JSSResource'
end

# Get info from JSS
comp = JSS.get(%</computers/macaddress/#{macAddress}/subset/Location>)
["computer"]

## Some code to update necessary values in retrieved hash above...

# Then I want to submit the changes back to the service, here's where
I'm struggling...
JSS_Submit.put(%</computers/id/#{comp["general"]["id"]}>,:body =>
JSON.dump(comp))

-------------------------

XML File and Curl command that work:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<computer>
<location>
<username>userName</username>
<real_name>Real Name</real_name>
<department>Department Name</department>
<building>Building Name</building>
</location>
</computer>


curl -u username:password https://casper.csf.bc.ca:8443/JSSResource/computers/id/8382
-T "/path/to/xml/file" -X PUT

-----------------------

Error I'm getting:

opening connection to casper.csf.bc.ca...
opened
<- "PUT /JSSResource/computers/id/3919 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Connection: close
Authorization: Basic RandomLetters=
Content-Length: 695
Host: servername

"
<- "{\"location\":{\"building\":\"POC\",\"position\":null,\"real_name
\":\"user Name\",\"username\":\"account_name\",\"phone\":null,
\"email_address\":null,\"room\":null,\"department\":\"Profs\"},
\"general\":{\"serial_number\":\"Serial_Number\",\"name\":\"CoputerName
\",\"jamf_version\":\"8.22\",\"distribution_point\":\"Server_Name\",
\"last_contact_time\":\"2011-10-21 12:18:51\",\"asset_tag\":null,
\"report_date\":\"2011-10-21 11:25:00\",\"alt_mac_address\":
\"MAC_Address\",\"id\":\"3919\",\"sus\":\"Server_Name\",\"mac_address
\":\"MAC_Address\",\"remote_management\":{\"managed\":\"true\",
\"management_password\":\"********************\",\"management_username
\":\"jss\"},\"barcode_1\":null,\"netboot_server\":\"Server_Name\",
\"barcode_2\":null,\"platform\":\"Mac\",\"ip_address\":\"IP_Address
\"}}"
-> "HTTP/1.1 400 Bad Request
"
-> "Date: Fri, 21 Oct 2011 19:24:54 GMT
"
-> "Accept-Ranges: bytes
"
-> "Server: Restlet-Framework/2.0.3
"
-> "Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
"
-> "Content-Type: text/html;charset=UTF-8
"
-> "Content-Length: 1098
"
-> "Connection: close
"
-> "
"
reading 1098 bytes...
-> "<html>
<head>
<title>Status page</title>
</head>
<body style=\"font-family: sans-serif;\">
<p style=\"font-size: 1.2em;font-weight: bold;margin: 1em 0px;\">Bad
Request</p>
<p>Error in field name {\"location\":{\"building\":\"POC\",\"position
\":null,\"real_name\":\"User Name\",\"username\":\"account_name\",
\"phone\":null,\"email_address\":null,\"room\":null,\"department\":
\"Profs\"},\"general\":{\"serial_number\":\"Serial_Number\",\"name\":
\"csf-s96-04076\",\"jamf_version\":\"8.22\",\"distribution_point\":
\"Server_name\",\"last_contact_time\":\"2011-10-21 12:18:51\",
\"asset_tag\":null,\"report_date\":\"2011-10-21 11:25:00\",
\"alt_mac_address\":\"MAC_Address\",\"id\":\"3919\",\"sus\":
\"Server_Name\",\"mac_address\":\"MAC_Address\",\"remote_management\":
{\"managed\":\"true\",\"management_password\":\"********************\",
\"management_username\":\"jss\"},\"barcode_1\":null,\"netboot_server\":
\"Server_name\",\"barcode_2\":null,\"platform\":\"Mac\",\"ip_address\":
\"IP_Address\"}}</p>
<p>You can get technical details <a href=\"http://www.w3.org/Protocols/
rfc2616/rfc2616-sec10.html#sec10.4.1\">here</a>.<br>
Please continue your visit at our <a href=\"/\">home page</a>.
</p>
</body>
</html>
"
read 1098 bytes
Conn close


-----------------------

Documentation for the "put" command I'm trying to use:

JAMF Rest API: PUT computers
Updates a computer in the JSS database, if the computer name or JSS id
matches. Returns the status of the update request. Fails if the
computer does not exist. Use POST to create a new Computer.

URL Formats
https://servername/JSSResource/computers/name/computer+name
https://servername/JSSResource/computers/id/computer+id

Requires authentication
Current credential: None
Generate a Credential

API Explorer
MetaInformation
Credential required. Updates a computer in the JSS database, if the
computer matches. Returns the status of the update request.

Update computer #13. Enter parameters: name=NewComputer


Parameters
Boolean valued fields are set to true if true is specified,
otherwise they are set to false.
When changing values such as Buildings that are defined in the JSS,
new values must be chosen from values already defined in the JSS. The
values are case-sensitive.
The computer id may not be changed since it is solely maintained by
the JSS
mac and ip addresses should be specified in a dotted notation, e.g.
ip_address=151.149.230.93
The management_password cannot be set in Version 7.2, thus is not
possible to create a computer that is managed. A computer created via
the API can be set to managed using the JSS administrative console.
Distribution point, software update server, and netboot server may
be set to none, or a valid value that exists in the JSS.
Attachments to computer purchasing information are possible in a
separate URI reference. See fileuploads for examples.
Little type checking is done for peripheral fields -- in particular
no checking is done to assure that peripheral menu choices are valid.

Status Codes
201 The request has succeeded.
400 The request has not succeeded because a parameter name is
incorrect.
401 Not authorized
403 The request has not succeeded because a parameter value is
incorrect.
404 Not found
409 The request has not succeeded because a parameter value is
incorrect.
An explicit error message for status 400, 403, and 409 is not
available in Tomcat at this time.

Curl Examples
curl -k -v -u username:password https://servername/JSSResource/computers/id/13
-T "file_example.xml" -X PUT
curl -k -v -u username:password https://servername/JSSResource/computers/name/computer%20name
-T "file_example.xml" -X PUT

Jeff

unread,
Nov 1, 2011, 6:47:18 PM11/1/11
to HTTParty Gem
Anyone? I've been struggling with this in my off minutes but so far
getting nothing. Can anyone give an example of submitting a hash as
XML to a REST service? I'm over my head here, but desperately trying
to finish a script to synchronize our management and inventory
systems. Thanks in advance.
Reply all
Reply to author
Forward
0 new messages