It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Answer accepted
Conor Atlassian Team Nov 23, 2015

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.

Conor Atlassian Team Nov 23, 2015

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
Conor Atlassian Team Nov 22, 2015

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 in Hipchat Cloud

Hipchat Cloud and Stride have reached End of Life (updated)

All good things come to an end - thanks to all our customers and partners who have been along the Hipchat and Stride journey with us.  As of Feb 15th 2019, Hipchat Cloud and Stride have reached ...

33,835 views 9 8
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you