Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Chad Yarber November 22, 2015

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
Atlassian Team members are employees working across the company in a wide variety of roles.
November 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
Atlassian Team members are employees working across the company in a wide variety of roles.
November 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
Conor
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 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.

Chad Yarber November 23, 2015

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

1 vote
Carlos Khatchikian
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 22, 2015

@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

 

 

0 votes
Chad Yarber November 24, 2015

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. 

0 votes
Carlos Khatchikian
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 23, 2015

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events