I am trying to post to a message string using json to our HipChat rooms. The json is formatted like this:
{Body:{"message":"StringMessage"}}
I am using this code to send the message:
string hcPostUrl = url + messageUrl + roomId.ToString() + messageUrl2 + messToken;
String hold = String.Empty;
using (var client = new WebClient())
{
var words = Newtonsoft.Json.JsonConvert.SerializeObject(json.getMessage());
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add(HttpRequestHeader.Accept, "application/json");
hold = client.UploadString(new Uri(hcPostUrl), "POST", words);
}
I have confirmed that the Url I am attempting to post to is correct and the token has the correct scope to send messages. What am I missing?
This is the error message I receive from Postman: "The request body cannot be parsed as valid JSON: Expecting value: line 1 column 2 (char 1)" My Json is set like this in Postman
{
"Body": {
"message": "words"
}
}
Edit in place of comment because I am not allowed due to lack of points (seriously that is stupid)
Tried Carlos's idea for json still receiving the 400. I also tried using this
{
"Body":{
"color":"purple",
"message_format":"html",
"message":"Test Test Test"
}
}
The following curl command works for me:
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" xxxxx.hipchat.com/v2/room/2/message?auth_token=XXXXXXX --data-binary '{"message":"test1"}'
The response to that was:
{"id": "e3530e5e-bc15-424f-8c1a-fbf976c651c7", "timestamp": "2015-11-24T05:57:24Z 460587"}
and the test message appeared in my room.
If I change that to use --data-binary '{Body:{"message":"StringMessage"}}' I see the same error you are seeing, which is caused by the fact Body is unquoted. With the Body tag quoted, I still get a 400 error but it is now due to fact the required field 'message' is missing. The Body component of your JSON is definitely incorrect and should not be present.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please try without the "Body" element - i.e. just send a
{"message": "..."}
unless the Body component is part of the C# machinery - I'm not familiar with C# json components.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have tried that with a number of different combinations for the message:body and I still get the 400
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Chad Yarber you should try to get the entire exception in the message it will tell you what is missing...
Without the payload that you sent is hard to know what is wrong.
You could use Chrome Extensions like POST man to mimic the call and verify what is wrong.
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
or the Advanced REST client
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you everyone for you help. Both Conor and Carlos were correct but I was sending the incorrect json format. It turns out that serializing the json using a converter was not the correct way to send the json to hipchat. All it wants is the information sent as a raw text string with minimal json formatting.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This should be a valid json: { "color": "purple", "message_format":"html", "message" : "some message here" }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.