Sending post request to S3 with RestAssured - java

I want to reproduce a postman call with RestAssured.
The call send some text parameters and a file parameter to upload file to S3
It look like this :
In rest assured I've done this :
Response response = RestAssured
.given()
.header("Content-Type","multipart/form-data")
.header("Connection","keep-alive")
.formParams(credentials) // credentials is a hashmap containing all the text params
.multiPart("file",new File("src/main/resources/media/desktop_1.webm"),"video/webm")
.post();
I'm getting this error NFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://s3HIDDENURL: Connection reset by peer: socket write error avr. 26, 2022 3:40:29 PM org.apache.http.impl.client.DefaultHttpClient tryExecute
I've also tried to change the file from multipart to formParam like this : .formParam("file",new File("src/main/resources/media/desktop_1.webm"),"video/webm")
I've got another error Cannot serialize because cannot determine how to serialize content-type multipart/form-data

Related

Dropwizard HttpClient Multipart http Request - Java

I am getting Multipart request from UI to upload csv file but call is going through one orchestrator service (dropwizard-version : 0.9.2) to target service (dropwizard-version : 1.2.2) for upload. While adding support for Multipart request in orchestrator service using MultipartRequestEntity, call is failing at target service with error -
[2022-03-08 16:23:44,474] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: 4c0693ee31713edf
! java.util.concurrent.TimeoutException: Idle timeout expired: 30005/30000 ms
! at org.eclipse.jetty.io.IdleTimeout.checkIdleTimeout(IdleTimeout.java:166)
! at org.eclipse.jetty.io.IdleTimeout$1.run(IdleTimeout.java:50)
Expected Request at Resource Layer for both services :
public void request(#FormDataParam("file") InputStream body,
#FormDataParam("file") FormDataContentDisposition fileDetails,
#FormDataParam("comment") String comment){}
Is there any solution for this?
In this case tried using JerseyClient instead of HTTP Client in orchestrator service for making call to target service, created temporary csv file in orchestrator service and sent updated Headers (Content-Type, Content-Encoding, Content-Length, Content-Language, Host) while making call to target service, which is working fine.

Difference in response headers - Normal Service vs Through Jetties reverse ProxyServlet

I am using standalone jetty application as reverse proxy, While sending a request to a service directly without using reverse proxy application, I get the following response headers with the response.
Content-Type →application/xml
Transfer-Encoding →chunked
Server →Jetty(9.3.8.v20160314)
When sending the same request using reverse proxy application, I get the following response headers with the response
Date →Fri, 04 Jan 2019 08:12:41 GMT
Content-Type →application/xml
Server →Jetty(9.3.8.v20160314)
Content-Length →460
The problem is that I get content-length header here instead of Transfer-Encoding. So whether reverse proxy does not send the response chunked or it stores the entire response and forwards it entirely.

Jersey 2 REST Client - Read Multipart Response / OctetStream Response

I'm building a Jersey 2 client, which calls a service to get a file from the server.
The service returns binary file content as application/octet-stream
NOw, this is my code where I call the webservice
Response response = target.request().header(HttpHeaders.COOKIE, this.cookie)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM)
.accept(MediaType.APPLICATION_OCTET_STREAM).get();
I get a HTTP 200 Response. But i don't understand how I can get the file content from the response.
EDIT
The service documentation says "To GET the binary file content and the metadata, use header Accept: multipiart/mixed"
So, I tried the below
Response response = target.request()
.header(HttpHeaders.COOKIE, this.cookie)
.header(HttpHeaders.CONTENT_TYPE, "multipart/mixed")
.accept("multipart/mixed").get();
Even here, I get a HTTP status 200 response. But How do I read the file content??
Please help!!
Take a look at the documentation you will see that response has a readEntity method that you can use to read the inputstream:
InputStream in = response.readEntity(InputStream.class);
... // Read from the stream
in.close();

Bad header in CXF

I'm working with CXF WebClient, i tried to do a webclient service and make the call with it, i set JSON type in header, but i getting the wild card in the header
I did this for make the webClient
client = WebClient.create(endPoint,Collections.singletonList(new JacksonJsonProvider())).
accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
ClientConfiguration config = WebClient.getConfig(client);
config.getInInterceptors().add(new LoggingInInterceptor());
config.getOutInterceptors().add(new LoggingOutInterceptor());
And i have this to make the get call
Response reponse=clientThreadSafe().path("tokens/{id}",virtualToken.getId()).get();
return genericReponse(Token.class,Status.OK,reponse);
With clientThreadSafe
private WebClient clientThreadSafe() throws CertEuropeException{
//thread safe, see http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety
return WebClient.fromClient(client);
}
And genericReponse
private <T> T genericReponse(Class<T> classReponse, Status status, Response reponse ) throws Exception{
if(reponse.getStatusInfo()!=status){
throw new Exception("somthing bad here");
}
return reponse.readEntity(classReponse);
}
But i getting the wildcard in the call
INFOS: Setting the server's publish address to be
http://localhost:9090 mars 14, 2016 1:52:31 PM
org.apache.cxf.interceptor.LoggingOutInterceptor INFOS: Outbound
Message
--------------------------- ID: 1 Address: http://localhost:9090/api/v1/tokens/1 Http-Method: GET
Content-Type: Headers: {Accept=[*/*]}
And i getting one exception
GRAVE: No message body reader has been found for class com.client.Token, ContentType: application/octet-stream
mars 14, 2016 1:52:31 PM
org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
AVERTISSEMENT: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type
I don't know why the WebClient is not taking the MediaType.APPLICATION_JSON header, maybe i don't use the right function for set the headers.
If i try with other rest client, like post man, and i set the right header all seem work fine.
After a lots of test, i found that the "Fluent interface" is not really working like it should, it seem that the order is important, and if you set the accept and the type of the WebClient at the beginning, this can be reset.
So for every call i have to made accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) after the path method, like:
path("tokens/{id}",token.getId())
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.invoke("GET", "")
If i change the order, the accept and type will not take in count

Sending POST request to a webservice

I want to send a POST request to test my local webservice. The request body is an XML body and i generated the classes using JAXB.
Everything is fine but when I execute the following code
ClientResponse response = service.path("api")
.path("v1")
.path("shipments")
.path("package")
.path("order")
.path("status")
.accept(MediaType.APPLICATION_XML)
.post(ClientResponse.class, orderStatusRequest);
I get the following error
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.api.shipments.model.OrderStatusRequestType, and MIME media type, application/octet-stream, was not found
Can someone point me to a direction, what i am missing here?

Categories