Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTP POST request with JSON String in JAVA
    primarykey
    data
    text
    <p>I have to make a http Post request using a JSON string I already have generated. I tried different two different methods : </p> <pre><code>1.HttpURLConnection 2.HttpClient </code></pre> <p>but I get the same "unwanted" result from both of them. My code so far with <strong>HttpURLConnection</strong> is:</p> <pre><code>public static void SaveWorkflow() throws IOException { URL url = null; url = new URL(myURLgoeshere); HttpURLConnection urlConn = null; urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoInput (true); urlConn.setDoOutput (true); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty("Content-Type", "application/json"); urlConn.connect(); DataOutputStream output = null; DataInputStream input = null; output = new DataOutputStream(urlConn.getOutputStream()); /*Construct the POST data.*/ String content = generatedJSONString; /* Send the request data.*/ output.writeBytes(content); output.flush(); output.close(); /* Get response data.*/ String response = null; input = new DataInputStream (urlConn.getInputStream()); while (null != ((response = input.readLine()))) { System.out.println(response); input.close (); } } </code></pre> <p>My code so far with <strong>HttpClient</strong> is:</p> <pre><code>public static void SaveWorkflow() { try { HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(myUrlgoeshere); StringEntity input = new StringEntity(generatedJSONString); input.setContentType("application/json;charset=UTF-8"); postRequest.setEntity(input); input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); postRequest.setHeader("Accept", "application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String output; while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>Where generated JsonString is like this:</p> <pre><code>{"description":"prova_Process","modelgroup":"","modified":"false"} </code></pre> <p>The response I get is:</p> <pre><code>{"response":false,"message":"Error in saving the model. A JSONObject text must begin with '{' at 1 [character 2 line 1]","ids":[]} </code></pre> <p>Any idea please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload