Receive 400 "Bad request" when attempting to Post message via C# app and Json

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"
    }
}

5 answers

1 accepted

3 votes
Accepted answer

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.

1 vote

@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

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en

 

 

1 vote

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.

I have tried that with a number of different combinations for the message:body and I still get the 400

0 votes

This should be a valid json: { "color": "purple", "message_format":"html", "message" : "some message here" }

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. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 14, 2018 in Hipchat Cloud

Hipchat Cloud and Stride End of Life: Just over 90 days away

Earlier this year we made the difficult decision to discontinue our team messaging tools, Stride and Hipchat. The end of life date for Stride and Hipchat Cloud is quickly approaching - February 15, 2...

501 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you