Glance content update proper json format?

php
hipchat

#1

Im trying to update a glance I have installed from my descriptor.

Im currently authenticating but returning an error 400 The method requires a json payload

Ive passed it all kinds of json and I cant figure out the format that it wants, even by looking at https://www.hipchat.com/docs/apiv2/method/room_addon_ui_update

Does anyone have a template of the json its actually looking for?

here is my code as well:
$json = “all kinds of json and none worked”;

$url = ‘https://api.hipchat.com/v2/addon/ui/room/{{room id}}’;

	$header = array();
	$header[] = 'Content-length: 0';
	$header[] = 'Content-type: application/json';
	$header[] = 'Authorization: Bearer {{my room token}}';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($json));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$resp = curl_exec($ch);
	$error = curl_error($ch);
	curl_close($ch);

#2

I’m looking at this now in PHP. PHP isn’t the greatest for complex JSON objects like you need with the Glance Room update API.

Some things I did to get past the “The request body cannot be parsed as valid JSON: Expecting value: line 1 column 1 (char 0)” error you were encountering.

  1. Remove the urlencode() around your $json in the CURLOPT_POSTFIELDS. This isn’t needed as it’s a post body and won’t be part of the URL. I used json_encode($json) to encode it but the JSON data I used was flat.
  2. I also removed the Content-length:0 in the header.

After removing those two things I got a different error: Required field ‘glance’ is missing

This is the right error to expect. It means the JSON I sent wasn’t formatted correctly (which in my tests it wasn’t).

Now I need to figure out how to craft a JSON object that has properties that are objects themselves.


#3

@rwhitbeck thanks for taking a look. I ended up getting to that same error Required field ‘glance’ is missing a few mins after I made this post. Ive thrown all kinds of json at it, mostly using json_encode($json). I had read something about being able to also pass query params (ex: message=‘foo’&param2=‘bar’) and then using url encode, so thats probably why thats there. It would pretty awesome if you could post your object as all mine have tanked so far. Thanks!

Edit/Addition:
This is the only json my glance uses btw. It just has a label and shows number of issues which gets returned from its endpoint. I have no conditions or other stuff.

{
“label”: {
“type”: “html”,
“value”: “XYZ Glance”
},
“status”: {
“type”: “lozenge”,
“value”: {
“label”: (string)$issues,
“type”: “error”
}
}
}


#4

I haven’t had time to finish figuring out the best way to get the JSON object passed in. Here is how I did it with a fairly basic object. Basically I had to create an array and then use json_encode to convert it to JSON object. I don’t understand why PHP is this bad at this. But maybe this is enough to get you going in the right direction. I assume it’ll be a mess of nested arrays.

<?php
$arr = array("message" => "Hey", "notify" => false);

$json = json_encode($arr);


$url = 'https://api.hipchat.com/v2/addon/ui/room/<room>';

	$header = array();
	$header[] = 'Content-type: application/json';
	$header[] = 'Authorization: Bearer <token>';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$resp = curl_exec($ch);
	$error = curl_error($ch);
	curl_close($ch);

  echo($error);
  echo($resp);
?>

#5

Yea its that json object thats still givving me trouble today. I get that “Required field ‘glance’ is missing” error still. I have 3 sets of json now that validated but dont work:

No good:
$json = '{
“glance”:{ “value”:“myaddon-cs-glance”},
“name”: {
“value”: “CS Support”
},
“queryUrl”: “https://test3rdparty.directnic.com/Hipchat/glance/cs”,
“key”: “myaddon-cs-glance”,
“target”: “cssidebar”,
“icon”: {
“url”: “https://dotology.com/local/dotology/img/contact_chat_icon.png”,
“url@2x”: “https://dotology.com/local/dotology/img/contact_chat_icon.png

}

}’;

No Good:
$json = ‘{
“glance”: {
“label”: {
“type”: “html”,
“value”: “XYZ Glance”
},
“status”: {
“type”: “lozenge”,
“value”: {
“label”: “”,
“type”: “error”
}
}
}
}’;

and then your simple:
$arr = array(“message” => “Hey”, “notify” => false);

$json = json_encode($arr);

also did not work.

Ive seen docs where it says it should match the pattern of the descriptor, but Im not finding that to be true.

Have you had a chance to look at the correct format? It would be a lifesaver.


#6

Ive been messing with the json format with no success. @rwhitbeck. Any luck?