Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpURLConnection Not Sending Post Data
    primarykey
    data
    text
    <p>I have been following the steps <a href="https://stackoverflow.com/a/2793153/620197">outlined here</a> for uploading files via the <code>HttpURLConnection</code>. In my case, there is POST parameter named "json," which needs to pulled out of <code>_POST['json']</code> on the server.</p> <p>The issue is, there is nothing in <code>_POST['json']</code>, except when I attempt to upload a file; the file data is placed in <code>_POST['json']</code>, instead of <code>_FILES['filename']</code>.</p> <p>The goal is to achieve this:</p> <pre><code>_POST['json'] = {"fileData":"Some extra data about the file(s)"} _FILES['filename'] = the file I uploaded. </code></pre> <p>My Code where I build the request is:</p> <pre><code>try { url = new URL(_requestObject.getUrl()); conn = (HttpURLConnection)url.openConnection(); conn.setRequestProperty("Content-Type", MMWebAPIConstants.MM_REQUEST_CONTENT_TYPE_HF + MMWebAPIConstants.MM_REQUEST_BOUNDARY); conn.addRequestProperty("Connection", "Keep-Alive"); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); if ( ! _requestObject.getAction().equals(MMWebAPIConstants.ACTION_LOGIN) ) { for ( String cookie : MMSessionManager.getSharedInstance().getCookies() ) conn.addRequestProperty("Cookie", cookie); } outstream = conn.getOutputStream(); writer = new PrintWriter(new OutputStreamWriter(outstream, "UTF-8"), true); writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf); writer.append("Content-Disposition: form-data; name=\"json\"").append(crlf); writer.append("Content-Type: text/plain; charset=UTF-8").append(crlf); writer.append(postValue.toString()).append(crlf).flush(); if ( hasfiles ) { byte[] bytes = null; InputStream input = null; for ( String filename : _requestObject.getFilePaths() ) { String filepath = this.getContext().getFilesDir().getAbsolutePath() + "/" + filename; String type = URLConnection.guessContentTypeFromName(filename); bytes = new byte[5120]; input = new FileInputStream(filepath); writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf); writer.append("Content-Disposition: form-data; name=\"" + filename + "\"; filename=\"" + filename + "\"").append(crlf); writer.append("Content-Type: " + type).append(crlf); writer.append("Content-Transfer-Encoding: binary").append(crlf); writer.append(crlf).flush(); for ( int l = 0; (l = input.read(bytes)) &gt; 0; ) { outstream.write(bytes, 0, l); } outstream.flush(); writer.append(crlf).flush(); } } writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY + "--").append(crlf); responseObject = this.getResponse(conn); this.getCookiesFromRequest(conn); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if ( writer != null ) writer.close(); if ( conn != null ) conn.disconnect(); } </code></pre> <p>And the post and files array looks like:</p> <pre><code>ERROR - 2012-08-02 18:49:59 --&gt; POST: Array ( [json] =&gt; PNG IHDR#JzsBIT|d 4IDATx͒7rcd).`Tv-IHMH @55k.c_@qّ)f3Zz*UK%귿6}OGI~IJ~:M|@eӼXw|0`?b Fլq^$zAn.;Cضo= 9J2O; e:=Md?DI{a;Lr56xA+~ҍZO|{orB{Iu&lt;VkWL&lt;x&lt;m=+ȡhE5$:ͲI\k `9_5Ofx7EX"D#'NO:ƧjS+uj:gjP=,rɳW3I s&gt;DM4ޖ)մ_*``@JZ6(ʝZ3\ u5삮*rת@Rkc$o=JRs$%aϮi]ځl;]Q7lI6K \ `L?OAsشZQOٍߍ*Y7VyoZuJYq,iM`SyYUSJfjѮf["0%y5N_0Z5"\ߚ$/fC!-ca^Mpfizvߚ???,)ak"_bUE&gt;ml,BcMSB[7y')O7I7c&gt;m3;Or6]qrAfe8t]I&gt;0Zc!6Ϡ8 YT0\YA\?&lt;#'GXGI^c"&lt;Ͳ]cU;k$'y/z$Ƣ&amp;N g&lt;-0Џr'IRJAŗ9|ʉj3IL[Yo&lt;fugq2;-acY[cw/Ô#,U /X"0x,k&gt;uF&amp;LRN$snu;דO ~M&lt;/aSQ[Sy˶&lt;*z`(RByP4Wh `L1=X s3ٖ'9VaJ WML 0&amp;|k:MB|LRNĚU&lt;S|z`AWt7010&amp;)'v/z%{cn=6lU9dW]"۔Ϫ&lt;LFH_cNoy.0&amp;SNT"fR.{0* `l&lt;RKr&lt;Ͳ?O;ÔxyػI~lU;=Jr@] yMa&amp;y=W}cbrƬs)aO_!&lt;`삣$ea. jUC~ơ+=6Zu-!8HfJ.B `@MRNDW }\wlҽ$$ߏ=`@]%ɿ9 Uɓq͗g)!Ъ*b!0H `4q|=LVO7&gt;#MW*yp. A͔m_/srr|We!]} 7GI˫OojRBK&gt;gDK~@|܀W????[v9M8K9V4.Gtq܅'9+zؼU!U5z8:)Mr'9dy~ZB5IxE40 (Ƀ&amp;NRNk(46+ܩU;}5ZnGI(EpA;I.n2Cۣ'`4pj5A] ס &amp;ݱwb&lt;gxGY .nqS*͞0СiسjV4uUߴvB2 !NV~r[;i`@5M n,k:٬خI4ZԾ6uIt1.Xs [Jg8ZW$tqQ)ByM3i/moJ@1mo7XnFi=&amp;ͪWBU}Sp%i%xjMIENDB` ) ERROR - 2012-08-02 18:49:59 --&gt; FILES: Array ( ) </code></pre> <p><strong>UPDATE</strong>:</p> <p>As a trial, I tried following <a href="https://stackoverflow.com/q/8900337/620197">this thread</a> (which threw a runtime exception), and I tried without the file upload, which does not populate <code>_POST['json']</code>, but still threw a runtime exception.</p> <p>Sill looking for some help on this please.</p>
    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