Java jax-rs send HTTP GET Request accompanied with JSON Data Input - java

I'm using jax-rs library to create a client testing a Web Service by sending HTTP requests to it, for study purposes. Now a POST Request looks something like this:
Response res = target.
request().post(Entity.entity(jinput.toString(), MediaType.APPLICATION_JSON));
and GET Request something like this:
Response res = target.
request().accept(MediaType.APPLICATION_JSON).get(Response.class);
Where target is the targeted URI and jinput the JSON Input used to post a resource...
Now I wonder... is there a way I can send a GET Request accompanied with a JSON input? For the sake of testing how the Web Service reacts to it...
By using cURL to manually send HTTP requests I can do it... but is there a way I can do it using the jax-rs library?
Thanks in advance :)
P.S. I would like to test the same thing with a DELETE Request as well... if possible...

Related

How to receive and parse a JSON object from a HTTP POST request from client using java on the server side

I am working on an iOS app and the server-side needs to receive POST from the client. The client-side is programmed in swift and HTTP requests are sent using the Alamofire library. The connection is clear and status code 200 is returned automatically by the server each time a POST request is sent. The content of the POST request is also correct. But how do I receive and parse the JSON object sent from the POST request by the client on the server-side? Certain actions on the server need the data from the POST requests.
The server side is written in Java, and I am using windows server 2019 on an Amazon EC2 server. Some clear directions would be great!
For java application, I suggest u First use spring initialization to create a maven and spring boot project , with spring mvc framework in it.
Second write a spring mvc controller with post method, which serve the request from IOS. This is am example:
#PostMapping("/xxx")
public XXX addXXX(#Valid #RequestBody XXXAddForm xxxAddForm) {
return xxxService.addXXX(xxxAddForm);
}
// this is the json object, which matches the format of your client side
public class XXXAddForm {
}

Getting data from post request in GWT client

I am trying to handle POST request in GWT. I did that in server side, but, i feel its work will be fine if the POST request is handled in client side. I was able to complete GET request in client side by reading the query string. But in case of POST, a file is being sent. How can i able to get the file from POST request in GWT client.
Thanks.
handling post request in GWT client is not possible. refer :
https://groups.google.com/forum/#!topic/google-web-toolkit/LOWMIPYcd7A

Java HTTP/HTTPS post from app 1 to app 2 in different server

I have to develop a java application which has to do a http post to a different application. Both the application does not have a JSP page as a front end. (Http POST from servlet to servlet).
When I was exploring options for this I managed to find
response.sendRedirect("http://localhost:8081/appname/servletname?var1=&var2=?...);
But this is based on http get. How do I do the same using HTTP post?
Thanks
Arun Kumar
The code you found is for redirecting an incoming request to another location. The client ends up sending a new request to the new URL. This is not what you want.
You can make an HTTP request using the class java.net.URL. You can then get an OutputStream and write your POST data in the body of the request. You will have to encode all your data values with java.net.URLEncoder.
I think there is a library in Apache commons that makes this easier.

Getting post data from request

I'm writing a server side app in Java using the HttpCore library.
I have an HttpRequest and I'm trying to get the postdata sent from a form. The problem is- when I use request.getEntity() it returns a null object, even though when I look through HTTPFox on what kind of request I'm sending the post data is clearly there.
What am I doing wrong?
There seems to be some confusion. You are sending requests from a browser to the server. The server is likely using the servlet API. There you handle requests using the doPost(..) method of an HttpServlet. You have an HttpServletRequest from which you can get the parameters - request.getParameter("paramName")
HttpCore on the other hand is used to make requests, not to handle requests. It is used as an http client (in the role of the browser).

Google protocol buffers and servlets

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?
Any suggestion or idea will be very much appreciated.
Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.
The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.
Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.
I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.
HI,
I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

Categories

Resources