Private Message User v2 link returning 404?

php
hipchat
rest-api

#1

So Im trying to write a function to PM a user when some code completes. Right now Im bare bones and just hard coding all values.

My issue is that when I curl the URL, enter it in a browser, or call my function from an action menu item, I get:

{
  "error": {
    "code": 404,
    "message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
    "type": "Not Found"
  }
}

Im using the docs at https://developer.atlassian.com/hipchat/guide/sending-messages

Here is my code, can anyone tell me if I have a bad URL or something? Thanks!

$url = 'http://api.hipchat.com/v2/user/@myusername/';
$msg = json_encode['message'=>'hey'];
$msg = urlencode($msg);
$personal_token = {personal token};

    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url."?auth_token=".$personal_token);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Access-Control-Allow-Origin", "*"));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$resp = curl_exec($ch);
	$error = curl_error($ch);
	curl_close($ch);

i return the $resp or the $error, whichever I get from there.
Im able to curl notification to my room, get data from my room and all kinds of other stuff, Im not sure why this is giving me a 404.


#2

I was able to reproduce your problem with a Bash script using HTTPie. So I think the problem is the wrong URL path. You have ${HIPCHAT_BASE_URL}/v2/user/@${HIPCHAT_USERNAME} but, to send a message, you should use ${HIPCHAT_BASE_URL}/v2/user/@${HIPCHAT_USERNAME}/message, per the docs on how to private message user.

A working example:

#!/usr/bin/env bash

http POST ${HIPCHAT_BASE_URL}/v2/user/@${HIPCHAT_USERNAME}/message \
  Authorization:"Bearer ${HIPCHAT_API_TOKEN}" \
  message="Notify me"

I prefer sending token in the header, but it should work the same for URL parameter.


#3

That wrong URL path was a mistake I made when I re-edited the post, sorry about that. I cannot install httpie here at work, so I cant replicate your solution.
To clarify and ask again though:

here is my current:

$url = ‘http://api.hipchat.com/v2/user/@username/message?auth_token={personal token}’ ;

with: $json = json_encode[‘message’=>‘hey’];

            $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Access-Control-Allow-Origin", "*"));
	
	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);

Im getting back this now: The request requires a properly encoded body with the ‘content-type’ header set to '[‘text/plain’, ‘application/json’]

but if I add in
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/plain’));

I get the same error.

How to fix this?


#4

For me, I would get a 404 trying to send the token via the querystring parameter. Once I added it to my header as @ibuchanan describes it worked for me.


#5

Ok guys, almost there. Ive redone my curl a little as per your posts and am now getting:
“code”: 400,\n “message”: “Field ‘message’ is required”,\

I have json_encoded, http_build_query()'ed, urlencode’ed, and typed the literal json into the curl_setopt($ch, CURLOPT_POSTFIELDS, {{my message here}}); and all have come back with the above message of Field Message is required.

Is there some better way to pass basicaly {“message”:“hey”} into that curl postfield? I thought for sure it would be looking for json, but apparently Im doing something wrong.

thanks for the prior help,
Please advise! Thx!


#6

@rwhitbeck @ibuchanan Ive got this one licked. But I do still have one question about encoding. First, the code that worked:

$url = ‘http://api.hipchat.com/v2/user/@’.$user[‘username’].’/message’ ;

$json = ‘{“message”:“Issue '.$id .”, Desc: “.$desc .” Completed" .’",“message_format”:“html”, “notify”:“true”}’;

            $header = array();
	$header[] = 'Content-type: application/json';
	$header[] = 'Authorization: Bearer '.$personal_token;
            $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           $resp = curl_exec($ch);
         $error = curl_error($ch);
         curl_close($ch);

So my question is now two fold:

  1. My $desc variable may have all kinds of stuff in it at any given time. Id really like to echo that back to the PM. I can get it to go with urlencode($desc) but that leaves nastiness in the PM. If I do htmlentities($desc) I get no PM, the message fails to send all together. According to the description for message format
    in the v2/method/private_message_user docs (sorry, it wont let me post the full link again) htmlentities() should do the trick. Why is this not the case? How to handle?

  2. My user is not getting notified even though I have notify set to true.

Looking forward to the answers!