0

When my page gets hit from a third party page, I get the below data in request payload:

Content-type: multipart/form-data, boundary----------14048
Content-Length = 590

----------14048
Content-disposition: form-data; name ="xyz"

{"abc":"lmn","def":"ghi"}
----------14048

I need to read the JSON string from this parameter in my Java class. How can I do that?

My current code looks like this:

IRequestParameters requestParameters = getRequest().getPostParameters();
    if (requestParameters != null && requestParameters.getParameterNames().contains( "abc" )&&requestParameters.getParameterValue( "abc" ) != null){
        value = requestParameters.getParameterValue( "abc" ).toString();
}

Thanks in advance.

Kylar
  • 8,876
  • 8
  • 41
  • 75
ayujain7
  • 11
  • 2
  • what does your current java spring code look like to read this post data ? – David Jeske Apr 15 '17 at 06:55
  • IRequestParameters requestParameters = getRequest().getPostParameters(); if (requestParameters != null && requestParameters.getParameterNames().contains( "abc" )      & requestParameters.getParameterValue( "abc" ) != null){         value = requestParameters.getParameterValue( "abc" ).toString(); } – ayujain7 Apr 15 '17 at 07:13
  • Please edit the question to add this code, don't paste it into a comment. Why are you asking for parameter "abc", when your form variable is called "xyz"? – David Jeske Apr 15 '17 at 07:39
  • My approach was - json is a key value pair, so i can get the value(lmn) by using key(abc) directly. Will I get the whole json string if i write-. requestParameters.getParameterValue("xyz").toString() – ayujain7 Apr 15 '17 at 07:51
  • Yes, you will get the whole JSON string with getParameterValue("xyz") .. Then you must decode the JSON using JSONParser() and then ask the parsed JSON object for "abc". – David Jeske Apr 16 '17 at 16:09
  • btw, it sounds ilke you are trying to read JSON data in Java Swing, posted by React... If that's the case, this question doesn't really have anything to do with react... – David Jeske Apr 20 '17 at 03:03
  • Actually, I was getting the data in the request payload and not as post parameters, because of which I had to capture the HttpRequest and use InputStream to read the payload and catch the line having json string. I shall update the question accordingly. Thanks for your efforts. – ayujain7 Apr 20 '17 at 07:08
  • Possible duplicate of [Wicket 6 - Capturing HttpServletRequest parameters in Multipart form?](http://stackoverflow.com/questions/26515158/wicket-6-capturing-httpservletrequest-parameters-in-multipart-form) – David Jeske Apr 21 '17 at 15:01

2 Answers2

2

First, you need to parse multipart form data in Wicket:

MultipartServletWebRequest multiPartRequest = 
      webRequest.newMultipartWebRequest(getMaxSize(), "ignored");
// multiPartRequest.parseFileParts(); // this is needed after Wicket 6.19.0+
IRequestParameters params = multiPartRequest.getRequestParameters();

Then you need to parse the JSON fragment, one way to do that is by using org.json.

import org.json.*;

JSONObject jsondict = new JSONObject(params.getParameter("xyz");

Then you need to get the JSON parameter you are interested in:

string payload = jsondict.getString("abc");
Community
  • 1
  • 1
David Jeske
  • 2,306
  • 24
  • 29
-1

The below code works fine for me.

HttpSevletRequest request = (HttpSevletRequest )getRequest.getContainerRequest();
try{
 InputStreamReader inputReader = new InputStreamReader(request.getInputStream());
    BufferedReader reader = new BufferedReader(inputReader );
    for(String line;(line = reader.readLine())!=null;){
        if(line.contains("abc")){
            //perform task....
     }
    }
}catch(IOException e){
 //logs
}
ayujain7
  • 11
  • 2
  • While this code may work in your test case, it is not correct. What if the posted JSON fragment has newlines? You need the multipart form post to be properly parsed. Does this help? http://stackoverflow.com/questions/26515158/wicket-6-capturing-httpservletrequest-parameters-in-multipart-form – David Jeske Apr 21 '17 at 14:27