0

I have trouble converting an example cURL string (that works) to a meaningful resteasy expression. The cURL is:

curl -X POST -u admin:admin --data-binary "@tempfile.xml" http://localhost:8810/rest/configurations

I have:

public void sendPost(ConfigurationDTO config) throws JAXBException, FileNotFoundException {
    // client target is:  http://localhost:8810/rest
    ResteasyWebTarget target = getTarget();
    target.path("configurations");
    JAXBContext context = JAXBContext.newInstance(ConfigurationDTO.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    // this here produces exactly what I need
    marshaller.marshal(config, new File("test.xml"));

    MultipartFormDataOutput dataOutput = new MultipartFormDataOutput();
    dataOutput.addFormData("file", new FileInputStream(new File("test.xml")), MediaType.APPLICATION_OCTET_STREAM_TYPE);
    GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(dataOutput) {};


    Response response = target.request().post( Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
    response.close();
}

protected ResteasyWebTarget getTarget() {
    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target(UriBuilder.fromUri(restUrl).build());
    client.register(new AddAuthHeadersRequestFilter(user, pass));
    return target;
}

Throws HTTP.500, I don't have access to the server to see what happened.

Erki M.
  • 5,022
  • 1
  • 48
  • 74
  • First I'm missing the user-password information in your code. – Tomas Oct 03 '14 at 11:34
  • It is done in `getTarget()`, also GET requests do work, so no problem there. – Erki M. Oct 03 '14 at 11:37
  • Should I understand it that you can call GET requests via your RESTEasy client and it works? – Tomas Oct 03 '14 at 11:40
  • Yes. No problems with GET, works as expected. The client is composed in the same method (added to original post). Only POST-s return HTTP.500 – Erki M. Oct 03 '14 at 11:46
  • Is your RESTEasy client running on a diffent port than your REST service? If so, have you configured CORS header only for GET requests? – Tomas Oct 03 '14 at 11:47
  • I have not (manually) set the port for the client. I simply would like to post request to an external service using the RESTEasy client the same way as the docs suggests (the cURL expression) – Erki M. Oct 03 '14 at 11:55

2 Answers2

1

I would try to debug CORS in cURL (see How can you debug a CORS request with cURL?). In your case, it is a command:

curl --verbose -u admin:admin \
  -H "Origin: http://localhost:1234" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS \
  http://localhost:8810/rest/configurations

Please, replace localhost:1234 with a context path where your RESTEasy client is running on.

If the request is not successful, it means a problem with CORS configuration. The request is successful if the response contains Access-Control-Allow-Origin header.

Community
  • 1
  • 1
Tomas
  • 71
  • 4
  • The cURL-call is working, problem is the RESTeasy code. Also here can be no CORS problems as neither curl nor the RESTeasy client have a Same-Origin-Policy. – lefloh Oct 03 '14 at 16:28
1

cURL is sending the Content-Type: application/x-www-form-urlencoded when using the --data-binary argument. You are using MediaType.MULTIPART_FORM_DATA_TYPE (multipart/form-data) so I'd expect that your server does not accept the latter. RESTeasy then would throw javax.ws.rs.NotSupportedException: Cannot consume content type.

I don't understand why you are marshalling your entity to a file and passing this file to the RESTeasy client. Using e.g. a StringWriter your code could look like this:

StringWriter sw = new StringWriter();
marshaller.marshal(config, sw);
Response response = target.request().post(Entity.entity(sw.toString(), MediaType.APPLICATION_FORM_URLENCODED));

Is the server part also written by you? If you are only sending xml files application/x-www-form-urlencoded does not seem to be the best matching ContentType.

lefloh
  • 10,653
  • 3
  • 28
  • 50
  • Thanks for your input. Writing to a file was for debug purposes mostly. Server is not written by me, it is like a blackbox, I am just testing it as we consume it. I will try your suggestions on Monday. – Erki M. Oct 03 '14 at 20:17