0

My scenario is,

I have a text field with a button named as "Search", when I entered a value in the text field and click on search button, the value should write/written in existing JSON file. Please find the below code.

function search_criteria()
{
var inputData = document.getElementById("sel").value;
var fileDetails = 
{
    'fileName'  : 'sample.json',
    'fieldName' : 'field1',
    'inputValue' : inputData
}
$
.ajax({
    type : "GET",
    url : "/abc/rest/input_value/search_action",
    data : JSON.stringify(fileDetails),
    contentType : 'application/json',
    success : function(msgr) {
    },
    error : function(data) {
        alert("Inside error while adding input in search field"
                + data);
    }
});

}

Java code

@GET
@Path("/search_action")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response search_action(@Context HttpServletRequest request, 
JSONObject inputJsonObj)
        throws JSONException, FileNotFoundException, IOException {

    String relativeWebPath = "/TEMP/scenario_content/" + 
inputJsonObj.get("fileName").toString();
    String absoluteDiskPath = 
request.getServletContext().getRealPath(relativeWebPath);

    File file = new File(absoluteDiskPath);
    if(!file.exists())
    {
        file.createNewFile();
    }
    JSONObject json = new JSONObject();
    json.put(inputJsonObj.getString("fieldName"), 
inputJsonObj.getString("inputValue"));

    FileWriter fw = new FileWriter(absoluteDiskPath);
    fw.write(json.toString());

    return Response.status(Status.OK).entity("success").type(MediaType.APPLICATION_JSON)
            .header("X-Content-Type-Options", "nosniff").build();
}

I got the below error by using this code.

org.apache.coyote.http11.AbstractHttp11Processor 
process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at 
DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1045)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Also I got 404 Bad Request in Browser console. Kindly help me to fix this issue.

MoNa
  • 365
  • 4
  • 13
  • 31
  • 404 is "not found", 400 is "bad request". So is it a 404 error, or a bad request error? – Jeremy Thille Aug 02 '17 at 06:15
  • You give the code @Path("/search_criteria") but ajax call url : "/abc/rest/input_value/searchAction". Can you set DEBUG level for java log? It is written that error is logged in DEBUG. Also, take a look at the browser network debug tab to check your request. – wargre Aug 02 '17 at 06:15
  • Possible duplicate of [Tomcat 7.0.43 "INFO: Error parsing HTTP request header"](https://stackoverflow.com/questions/18819180/tomcat-7-0-43-info-error-parsing-http-request-header) – Abdullah Khan Aug 02 '17 at 06:16
  • Typo.. Its 400 Bad Request not 404.. – MoNa Aug 02 '17 at 06:31

1 Answers1

0

in the code that you are showing, the path for the Controller is @Path("/search_criteria") but in your ajax call your url is url : "/abc/rest/input_value/searchAction", so they don't macth and you get a 404.

Antonio314
  • 61
  • 6
  • I got the same error when I changed to /abc/rest/input_value/search_criteria – MoNa Aug 02 '17 at 06:42
  • Have you tried to write the url in the browser? Maybe is not an ajax problem. – Antonio314 Aug 02 '17 at 06:50
  • I have rectified the error by changing POST method in java code. Previously I have used GET method. – MoNa Aug 02 '17 at 13:44
  • But the value is not appended in the existing json file, even though I got 200 OK message in Browser console. – MoNa Aug 02 '17 at 13:49