Ajax: Sending multiple parameters in HTTP POSTrequest [duplicate] - java

This question already has answers here:
How to send simple http post request with post parameters in java
(5 answers)
Closed 7 years ago.
How to send multiple parametrs in HTTP POST request?
I am sending POST request through Ajax call to servlet
I tried below two ways but neither worked
Approach 1:
httpRequest.open("POST", "http://localhost/Servlet/ps" , true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
.
.
.
httpRequest.send("msg="+data & "TC="+TC);
At Servlet:
String data = req.getParameter("msg"); // this prints value fine
String TestCaseNo = req.getParameter("TC"); // this prints null
Approach 2:
httpRequest.open("POST", "http://localhost/Servlet/ps" , true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
.
.
.
httpRequest.send("msg="+data + "TC="+TC);
ANSWER:This is how it worked for me
var value="msg="+data+"&TC="+TC;
http-Request.send(value);

I'm unfamiliar with AJAX but this seems more like a setup for GET request httpRequest.send("msg="+data & "TC="+TC).
Try writing parameters to your connection output stream and then in doPost method get it from req.getInputStream(). This will definitely work, but idk if it's exactly what your asking for.

Related

Servlet get full path of request with parameters [duplicate]

This question already has answers here:
HttpServletRequest to complete URL
(12 answers)
Closed 9 months ago.
I'm using a Servlet accessible via POST request, but I've seen that parameters in POST, can be set either thanks to header parameters but also in a GET format way (/MyServlet?param1=123&param2=456) and I would need to detect it on my Servlet.
I tried to retrieve the request by using
request.getRequestURI()
but I cannot see the parameters in that case...
Do you know how can I retrieve the full path of the request in a GET parameter way ?
getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String.

How do I verify two post requests to the same url but with different bodies in Wiremock? [duplicate]

This question already has answers here:
Wiremock: Multiple responses for the same URL and content?
(2 answers)
Closed 17 days ago.
How do I verify two post requests to the same url but with different bodies in Wiremock? The same url should be called once with body 1 and once with body 2.
As it is now Wiremock only cares about verifying the last line.
verify(postRequestedFor(urlEqualTo("/my-url"))
.withRequestBody(equalToJson(resourceAsString("my-first-body.json"), true, false)));
verify(postRequestedFor(urlEqualTo("/my-url"))
.withRequestBody(equalToJson(resourceAsString("my-other-body.json"), true, false)));
I ended up doing this:
var postRequests = findAll(postRequestedFor(urlMatching("/my-url")));
assertThat(postRequests.get(0).getBodyAsString()).isEqualTo(resourceAsString("my-first-body.json"));
assertThat(postRequests.get(1).getBodyAsString()).isEqualTo(resourceAsString("my-other-body.json"));

Java Requests with json data [duplicate]

This question already has answers here:
HTTP POST using JSON in Java
(12 answers)
Closed 1 year ago.
I want to make an HTTP request with Java,
but I'm new to Java and have no clue how.
I've had a look at a few tutorials,
but I was unable to understand anything.
I want to send JSON data and also receive JSON data.
In Python it would look like this:
response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson)))
Any help would be much appreciated.
Use the below link for reference
https://www.baeldung.com/java-http-request
You can use rest template also if you're using external library.
ResponseEntity<> response = restTemplate.exchange(
UriComponentsBuilder.fromHttpUrl(baseurl + "jobs").toString(),
HttpMethod.POST,
new HttpEntity<>(body,headers),
<someClass>.class);

for sendredirect which method gets called doGet or doPost() [duplicate]

This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
I am new to servlets.
My Q is if I use for response.sendredirect()
which method gets called doGet or doPost()?
I know that in jsp to servlet get or post method will get called according to method type.
But if it is servlet to servlet request using response.sendRedirect() which method will get called?
how servlet engine decides which method to call?
Thanks in avdance.
redirect is always use get method,
redirection means a new request..
when we give send redirect actually happening is a new request from the user..
and it is always get..
since it is a new request we cant access the old request parameters
response.sendRedirect is always a GET
A sendRedirect() is always a 2 step process in which the server sends a URL Location and a status code of 301 to client browser.
The client browser then GET's the URL and then goes to that url location.(You can see this url in the address bar).
Remember a request to a Http or a URL link is always a Get request whether the URL is to a servlet within application or to external location.
Refer
http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletResponse.html#sendRedirect%28java.lang.String%29

Send Http Post Message using a stream in Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use java.net.URLConnection to fire and handle HTTP requests?
I have to send an http post message to a server.
This message must have an header and a body and to send it i must use a Stream.
Both the header and the body must be an array of Byte.
How can I do this?
Thank you
From the server you can get header information separately from HttpServletRequest.getHeaders(). Data should be read in the form of parts (multipart/form-data) using input stream that can be opened from Request object. Hope this help.
Ref:
http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html
http://www.jguru.com/faq/view.jsp?EID=1045507

Categories

Resources