Unable to make a post call using httpconnectionurl and outstreamwriter - java

Below is the code i am testing with. When i run this i see that post body is empty. i can see all my header when i trace on the server side but the body is empty. Can you please point out my mistake here ?
String jsonstring = "{\"id\":\"1233\", \"userName\" : \"jump2\"}";
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonstring);
System.out.println(jsonObject.toJSONString());
Long currentTime = System.currentTimeMillis();
URL restUrl = new URL("http://myservers.com/v1/register");
HttpURLConnection urlConnection = (HttpURLConnection) restUrl.openConnection();
urlConnection.addRequestProperty("X-CORRELATION-ID", currentTime.toString());
urlConnection.addRequestProperty("X-AUTH-APIKEY", "dDEdfaeFFDdddDF");
urlConnection.addRequestProperty("Accept","application/json");
urlConnection.addRequestProperty("Content-Type","application/json");
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
jsonObject.writeJSONString(outputStreamWriter);
System.out.println(urlConnection.getResponseCode());
System.out.println(urlConnection.getRequestMethod());
System.out.println(urlConnection.getContentEncoding());
outputStreamWriter.flush();
outputStreamWriter.close();
urlConnection.disconnect();```

You have to set Request method to POST
urlConnection.setRequestMethod("POST");
Example for HttpUrlConnection.

Related

JSON data missing while sending to server

When I parse an JSON object, it gets parsed easily and in the correct format. But as soon as I send the same thing to the server side, values go missing.
JSONArray check0 = new JSONArray();
JSONArray check1 = new JSONArray();
JSONArray check2 = new JSONArray();
check0.put(v11);
check0.put(v12);
check0.put(v13);
check1.put(c11);
check1.put(c12);
check1.put(c13);
check4.put(t11);
check4.put(t12);
check4.put(t13);
draft.put("check2",id11);
draft.put("check3",t11);
draft.put("check0",check0);
draft.put("check1",check1);
draft.put("check4",check4);
where v11,v12.......t12,t13 are string variables.
My code for sending data:
URL url = new URL("http://10.0.2.2:8080/initial");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
connection.setDoOutput(true);
DataOutputStream dStream = new
DataOutputStream(connection.getOutputStream());
System.out.println(draft.toString());
dStream.writeBytes(draft.toString());
System.out:
{"Check1":["5","4","4"],"Check0":["6","44","4"],"Check2":"17082017123406","Check4":"1228123682","Check3":["4","4","4"]}
Code to receive the post request:
app.post('/initial', function(req, res){
console.log("POST received: "+ JSON.stringify(req.body));
res.end("cool");
});
Console output
POST received:
{"{\"Check1\":":{"\"5\",\"4\",\"4\"":{"\"6\",\"44\",\"4\"":{"\"4\",\"4\",\"4\"":""}}}}
try using the following
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
Example
private JSONObject uploadToServer() throws IOException, JSONException {
String query = "https://example.com";
String json = "{\"key\":1}";
URL url = new URL(query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
os.write(json.getBytes("UTF-8"));
os.close();
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
JSONObject jsonObject = new JSONObject(result);
in.close();
conn.disconnect();
return jsonObject;
}

Whole JSONObject sent as JSON key using HttpURLConnection in Android

I want to send a POST request to my NodeJS/Express API with some JSON data. I managed to make GET requests with no problem. Here's what I do for a POST request:
URL u = new URL(url);
JSONObject jsonObject = new JSONObject();
jsonObject.put("nombre", "testing");
c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);
c.setRequestMethod("POST");
c.setRequestProperty("Content-length", Integer.toString(URLEncoder.encode(jsonObject.toString(),"UTF-8").length()));
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(0);
c.setReadTimeout(0);
c.connect();
DataOutputStream printout = new DataOutputStream(c.getOutputStream ());
printout.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-8"));
printout.flush ();
printout.close ();
int status = c.getResponseCode();
In my API, I have a console.log(req.body); to see what is my POST route getting, and this is what I get in my console:
Got this for POST:
{ '{"nombre":"testing"}': '' }
The whole JSONObject is sent as the key for the JSON object with an empty value on the HTTP POST request. Any ideas on what am I doing wrong? Thanks!
Try replace
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
with
c.setRequestProperty("Content-Type", "application/json");
and
printout.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-8"));
with
printout.writeBytes(jsonObject.toString());

Paypal update invoice rest api HTTP response code: 500

i am currently trying to use REST api provided by Paypal to create my own service using servlet. I manage to transfer the cURL code into HttpsURLConnection using java.
Here is my code:
JSONObject returnJson = new JSONObject();
PrintWriter out = response.getWriter();
JSONParser jparser = new JSONParser();
try{
String inputStr = request.getParameter("input");
System.out.println(inputStr);
JSONObject inputJson = (JSONObject) jparser.parse(inputStr);
String accessToken = (String) inputJson.get("access_token");
String invoiceId = (String) inputJson.get("invoiceId");
String url = "https://api.sandbox.paypal.com/v1/invoicing/invoices/"+invoiceId;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("PUT");
con.setRequestProperty("Accept-Language", "text/html; charset=UTF-8");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer "+accessToken);
//Tentatively, the input is hard coded, after integration, the input comes from http request.
//However, only merchant email in mandatory for invoice creation in sand box so far
//For details of invoice attributes please refer to this link--> https://developer.paypal.com/docs/api/#update-an-invoice
String urlJsonString = "{\"id\":\""+invoiceId+"\",\"status\":\"DRAFT\",\"merchant_info\":{\"email\":\"rui.song.2013-facilitator#sis.smu.edu.sg\",\"first_name\":\"Dennis\",\"last_name\":\"Doctor\",\"business_name\":\"MedicalProfessionals,LLC\",\"phone\":{\"country_code\":\"US\",\"national_number\":\"5032141716\"},\"address\":{\"line1\":\"1234MainSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97217\",\"country_code\":\"US\"}},\"billing_info\":[{\"email\":\"sally-patient#example.com\"}],\"shipping_info\":{\"first_name\":\"Sally\",\"last_name\":\"Patient\",\"business_name\":\"Notapplicable\",\"address\":{\"line1\":\"1234BroadSt.\",\"city\":\"Portland\",\"state\":\"LALA\",\"postal_code\":\"97216\",\"country_code\":\"US\"}},\"items\":[{\"name\":\"Sutures\",\"quantity\":100,\"unit_price\":{\"currency\":\"USD\",\"value\":\"250\"}}],\"invoice_date\":\"2014-01-07PST\",\"payment_term\":{\"term_type\":\"NO_DUE_DATE\"},\"tax_calculated_after_discount\":false,\"tax_inclusive\":false,\"note\":\"MedicalInvoice16Jul,2013PST\",\"total_amount\":{\"currency\":\"USD\",\"value\":\"250\"}}";
System.out.println(urlJsonString);
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(urlJsonString);
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
out.print(responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer res= new StringBuffer();
while ((inputLine = in.readLine()) != null) {
res.append(inputLine);
}
in.close();
returnJson = (JSONObject) jparser.parse(res.toString());
System.out.println(returnJson);
}catch(Exception e){
e.printStackTrace();
returnJson.put("message", e);
}
out.print(returnJson);
I am testing the service on localhost, and i will manually pass in TWO parameters: "access_token" and "invoiceId" like this:
http://localhost:8080/Authentication/PaypalUpdateInvoiceServlet?input={"access_token":"A015Rv3XNo4fmFh4JC2sJiGjl1oEQ5w-B9azU.H6nlzMm1s","invoiceId":"INV2-9TRP-2S2R-OPBD-XK9T"}
These two pieces of info are obtained by me using the similar code i mentioned above.
I only modified codes in the entier HttpsURLConnection part to correspond with the cURL request and response sample provided in Paypal site. Link -->(https://developer.paypal.com/docs/api/#update-an-invoice)
Thus far, i successfully implement Create, Retrieve for invoice. I use the same way to make the servlet call with the specific parameters required and are able to get the expected response show on Paypal site.
BUT Now i am stuck with update invoice. When i make the servlet call.
i will receive:
500{"message":java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.sandbox.paypal.com/v1/invoicing/invoices/IINV2-9TRP-2S2R-OPBD-XK9T}
Can anyone help me explain why i get this error and how shall i fix this?

Java HTTP POST request (JSON) to PHP server

I have an application (Java) that needs to send json to a php web service.
This is my method to send User in JSON :
public void login(User user) throws IOException {
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json);
String url = "http://localhost/testserveur/index.php";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection)obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("json", json);
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.flush();
}
int responseCode = con.getResponseCode();
System.out.println(responseCode);
}
And my php code :
$string=$_POST['json'];
I tried to insert in my database but $_POST['json'] does not exist.
I didn't see you to posting anything. Add this to your code:
String param = "json=" + URLEncoder.encode(json, "UTF-8");
wr.write(param.getBytes());
This is not right:
con.setRequestProperty("json", json);
setRequestProperty is not used to set the HTTP payload. It is used to set the HTTP headers. For example, you should set the content type accordingly anyway. Like this:
con.setContentType("application/json");
The actual data that you are going to post goes into the HTTP body. You just write it to the end of the stream (before flush):
Here it depends on your implementation on the web server if the data needs to be escaped. If you read the body of the post and interpret it as JSON straight away, it does not need to be escaped:
wr.write(json);
If you transmit one or more JSON strings through parameters (which it looks like, since you are parsing it on the server like $_POST['json']), then you need to url-escape the string:
wr.write("json=" + URLEncoder.encode(json, "UTF-8"));
Im not very familiar with php. You might need to url-decode the string on the server before processing the received json-string any further.
Thx for your help.
This works :
public void login(User user) throws IOException {
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json);
String url = "http://localhost/testserveur/index.php";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("json", json);
OutputStream os = con.getOutputStream();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
//wr.write(new String("json=" + json).getBytes());
String param = "json=" + URLEncoder.encode(json, "UTF-8");
wr.write(param.getBytes());
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println(responseCode);
}
PHP :
$string=$_POST['json'];

Android HttpsURLConnection and JSON for new GCM

I'm overhauling certain parts of my app to use the new GCM service to replace C2DM. I simply want to create the JSON request from a Java program for testing and then read the response. As of right now I can't find ANY formatting issues with my JSON request and the google server always return code 400, which indicates a problem with my JSON.
http://developer.android.com/guide/google/gcm/gcm.html#server
JSONObject obj = new JSONObject();
obj.put("collapse_key", "collapse key");
JSONObject data = new JSONObject();
data.put("info1", "info_1");
data.put("info2", "info 2");
data.put("info3", "info_3");
obj.put("data", data);
JSONArray ids = new JSONArray();
ids.add(REG_ID);
obj.put("registration_ids", ids);
System.out.println(obj.toJSONString());
I print my request to the eclipse console to check it's formatting
byte[] postData = obj.toJSONString().getBytes();
try{
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpsURLConnection.setDefaultHostnameVerifier(new JServerHostnameVerifier());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + API_KEY);
System.out.println(conn.toString());
OutputStream out = conn.getOutputStream();
// exception thrown right here. no InputStream to get
InputStream in = conn.getInputStream();
byte[] response = null;
out.write(postData);
out.close();
in.read(response);
JSONParser parser = new JSONParser();
String temp = new String(response);
JSONObject temp1 = (JSONObject) parser.parse(temp);
System.out.println(temp1.toJSONString());
int responseCode = conn.getResponseCode();
System.out.println(responseCode + "");
} catch(Exception e){
System.out.println("Exception thrown\n"+ e.getMessage());
}
}
I'm sure my API key is correct as that would result in error 401, so says the google documentation. This is my first time doing JSON but it's easy to understand because of its simplicity. Anyone have any ideas on why I always receive code 400?
update: I've tested the google server example classes provided with gcm so the problem MUST be with my code.
{"collapse_key":"new-test-notification","data":{"info1":"info_1","info3":"info_3","info2":"info 2"},"registration_ids":["APA91bG3bmCSltzQYl_yOcjG0LPcR1Qemwg7osYJxImpSuWZftmmIjUGH_CSDG3mswKuV3AAb8GSX7HChOKGAYHz1A_spJus5mXFtfOrK0fouBD7QBpKnfc_ly0t3S8vSYWRjuGxtXrt"]}
I solved it using a different approach so it must have been my original implementation.
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username",username));
list.add(new BasicNameValuePair("deviceId",regId));
list.add(new BasicNameValuePair("deviceType","AndroidPhone"));
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("example.url.com");
post.setEntity(new UrlEncodedFormEntity(list));
ResponseHandler<String> handler = new BasicResponseHandler();
HttpResponse response = client.execute(post);
String responseString = handler.handleResponse(response);
JSONObject jsonResponse = new JSONObject(responseString);
I'm reading here that you should use your 'Key for browser apps'
GCM with PHP (Google Cloud Messaging) (in comments)
Try to reproduce the error using curl or another command-line tool so we can eliminate the possibility that there's a bug in your java that we're all missing.

Categories