Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTP POST does not return expected JSON response
    primarykey
    data
    text
    <p>I have pasted a code snippet for HTTP Post where I am POSTING a multipart message to the server which needs Authentication. I am expecting a JSON response, but when I run this I always get the login page in HTML.</p> <pre><code>public final class MyScreen extends MainScreen { private RichTextField _Output; public MyScreen() { // Set the displayed title of the screen setTitle("MyTitle"); _Output = new RichTextField(); add(_Output); addMenuItem(_GetDataAction); } protected MenuItem _GetDataAction = new MenuItem("GetData", 100000, 10) { public void run() { String URL = "&lt;Sample URL Goes Here&gt;"; ServiceRequestThread svc = new ServiceRequestThread(URL, (MyScreen) UiApplication.getUiApplication() .getActiveScreen()); svc.start(); } }; public void updateDestination(final String text) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { _Output.setText(text); } }); } } class ServiceRequestThread extends Thread { protected String _URL; protected MyScreen _Dest = null; protected URLEncodedPostData _PostData = null; StringBuffer writer = new StringBuffer(); public void setPOSTData(URLEncodedPostData data) { _PostData = data; } public ServiceRequestThread(String URL, MyScreen screen) { super(); _Dest = screen; _URL = URL; } public void run() { try { String boundary = "SATBA"; String twoHyphens = "--"; String data1 = "{\"IMPORTING\":{ \"IN_COUNTRY_CODE\":\"US\"}}"; String CRLF = "\r\n"; byte[] encoded = Base64OutputStream.encode ("User:password".getBytes(), 0, "User:password".length(), false,false); "Prepare the data for post" writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"param\"").append( CRLF); writer.append("Content-Type: text/json; charset=" + "UTF-8").append(CRLF); writer.append("Content-Transfer-Encoding: 8bit").append(CRLF); writer.append("Request-Id:Abcd123456" ).append(CRLF); writer.append("Request-Type:rfc_json").append(CRLF); writer.append("function:00163E0136C01EE0AE8B059433A71727") .append(CRLF); writer.append(CRLF); writer.append(data1).append(CRLF); writer.append("--" + boundary + "--").append(CRLF); String string = new String(writer); HttpConnection conn1 = (HttpConnection)Connector.open(_URL,Connector.READ_WRITE); conn1.setRequestMethod(HttpConnection.POST); conn1.setRequestProperty("Authorization", "Basic "+ new String(encoded)); conn1.setRequestProperty("Content-Type","multipart/mixed; boundary=" + boundary); OutputStreamWriter osw = new OutputStreamWriter(conn1.openOutputStream(), "UTF-8"); osw.write(string); osw.flush(); osw.close(); int responseCode = conn1.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { InputStream data = conn1.openInputStream(); StringBuffer raw = new StringBuffer(); byte[] buf = new byte[4096]; int nRead = data.read(buf); while (nRead &gt; 0) { raw.append(new String(buf, 0, nRead)); nRead = data.read(buf); } _Dest.updateDestination(raw.toString()); } else { _Dest.updateDestination("responseCode=" + Integer.toString(responseCode)); } } catch( IOException e) { e.printStackTrace(); _Dest.updateDestination("Exception:"+e.toString()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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