Reputation: 49
I'm trying to request POST for Google Firebase in server. I followed the document guideline, but it has not worked.
My sending message function is following thing.
private static void sendMsg() throws IOException
{
String url = "https://fcm.googleapis.com/fcm/send";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "key=XXXX");
JSONObject msg=new JSONObject();
msg.put("message","test8");
JSONObject parent=new JSONObject();
parent.put("to", "XXXXX");
parent.put("data", msg);
con.setDoOutput(true);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + parent.toString());
System.out.println("Response Code : " + responseCode+" "+con.getResponseMessage());
}
The response code is "411" and the message is "Length Required". I also tried to set the content length, but the result was same.
Am I doing wrong?
Upvotes: 2
Views: 875
Reputation: 38299
You have all the setup right but are not writing the data. Add the statements shown:
con.setDoOutput(true);
// Added
OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream());
os.write(parent.toString());
os.flush();
os.close();
Upvotes: 2