1

I am getting the following error

InvalidPolicyDocumentThe content of the form does not meet the conditions specified in the policy document.Policy document parsing error: Lexical Error: Unmatched Input: <s>

My Async Task to call a HTTPURLConnection is below. The multipartv1 class is from here.

Inside an AsyncTask doinbackground

{multipartv1.addFormField("key", filename_insert_value);
multipartv1.addFormField("bucket", “myapplication”);
multipartv1.addFormField("Content-Type", "image/jpeg");
multipartv1.addFormField("GoogleAccessId", “myapplication@appspot.gserviceaccount.com");
multipartv1.addFormField("acl", "bucket-owner-read");
multipartv1.addFormField("success_action_redirect", "https://myapplication.appspot.com/getpolicydocumentsuccess");
multipartv1.addFormField("success_action_status", "201");
multipartv1.addFormField("x-goog-meta-programname", this.current_program_name);
multipartv1.addFormField("x-goog-meta-membername", this.member_element_id);
multipartv1.addFormField("x-goog-meta-tag1", tag1);
multipartv1.addFormField("x-goog-meta-tag2", tag2);
multipartv1.addFormField("x-goog-meta-tag3", tag3);
multipartv1.addFormField("x-goog-meta-tag4", tag4);
multipartv1.addFormField("x-goog-meta-tag5", tag5);
multipartv1.addFormField("policy", this.policy);
multipartv1.addFormField("signature", this.signature);

multipartv1.addFilePart("file", new File(this.file_absolute_path));

List<String> response_multipart = multipartv1.finish();
Log.e(Tag, "SERVER REPLIED:");
for (String line : response_multipart) {
Log.e(Tag, "Upload Files Response:::" + line);
}

The "policy" is generated from my backend (Google App Engine) using Google Cloud Endpoint API. This is as policy document supported by Google Cloud Storage XML API as listed here. The "policy" works perfectly when accessed through a Webapplication.

The "policy" looks like this -

ewkJCQoJCQkJImV4cGlyYXRpb24iOiAiMjAxNi0wNi0xNlQxMToxMToxMVoiLAoJCQkJImNvbmRpdGlvbnMiOiBbCgkJCQkJWyJzdGFydHMtd2l0aCIsICIka2V5IiwgIiIgXSwKCQkJCQl7ImFjbCI6ICJidWNrZXQtb3duZXItcmVhZCIgfSwKCQkJCQl7ImJ1Y2tldCI6ICJlbmdhZ2VkLXBhcnNlYy02MTIuYXBwc3BvdC5jb20ifSwKCQkJCQl7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0Ijogc3VjY2Vzc19yZWRpcmVjdCB9LAoJCQkJCXsic3VjY2Vzc19hY3Rpb25fc3RhdHVzIjogIjIwMSJ9LAkJCQoJCQkJCVsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS9qcGVnIiBdLAoJCQkJCVsic3RhcnRzLXdpdGgiLCAiJHgtZ29vZy1tZXRhLXByb2dyYW1uYW1lIiwgIiJdLAoJCQkJCVsic3RhcnRzLXdpdGgiLCAiJHgtZ29vZy1tZXRhLW1lbWJlcm5hbWUiLCAiIl0sCgkJCQkJWyJzdGFydHMtd2l0aCIsICIkeC1nb29nLW1ldGEtdGFnMSIsICIiXSwKCQkJCQlbInN0YXJ0cy13aXRoIiwgIiR4LWdvb2ctbWV0YS10YWcyIiwgIiJdLAoJCQkJCVsic3RhcnRzLXdpdGgiLCAiJHgtZ29vZy1tZXRhLXRhZzMiLCAiIl0sCgkJCQkJWyJzdGFydHMtd2l0aCIsICIkeC1nb29nLW1ldGEtdGFnNCIsICIiXSwKCQkJCQlbInN0YXJ0cy13aXRoIiwgIiR4LWdvb2ctbWV0YS10YWc1IiwgIiJdLAoJCQkJCVsiY29udGVudC1sZW5ndGgtcmFuZ2UiLCAwLCAzMDAwMDAwMF0KCQkJCQldCQkJCQoJCQkJfQ==

My fear is when I add the parameter for "policy" in my HttpURLConnection, something is messed up. The code to add the "policy" parameter is this -

 public void addFormField(String name, String value) {
            writer.append("--" + boundary).append(LINE_FEED);
            writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
                    .append(LINE_FEED);
            writer.append("Content-Type: text/plain; charset=" + charset).append(
                    LINE_FEED);
            writer.append(LINE_FEED);
            writer.append(value).append(LINE_FEED);
            writer.flush();
        }

The HTTP Post call is here -

public List<String> finish() throws IOException {
        List<String> response = new ArrayList<String>();
        writer.append(LINE_FEED).flush();
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
        return response;
    }

The boundary string is this

   String boundary = "===" + System.currentTimeMillis() + "===";
Community
  • 1
  • 1
Jack tileman
  • 469
  • 5
  • 14
  • Can you post the full stack trace for the error shown? – Nick Feb 03 '16 at 23:42
  • solved this by breaking down the string to the following policy_doc = """{multipartv1.addFormField("key", filename_insert_value), multipartv1.addFormField("success_action_redirect";""" + "https://myapplication.appspot.com/getpolicydocumentsuccess" + """); multipartv1.addFormField("success_action_status", "201");""" – Jack tileman Feb 06 '16 at 04:22
  • Great to hear. You should post that as a self-answer now. – Nick Feb 08 '16 at 17:21
  • i'm getting same error how did you solved it. `Policy document parsing error: Lexical Error: Unmatched Input: ` – bhavin jalodara May 28 '18 at 21:10

0 Answers0