0

I'm very new to web-service dev and I'm trying to make a POST request to an API using Jersey. The issue is I think I'm mixing documentation and example I'm finding online between client & server. I'm pretty sure that it's simple but I can't figure out why my code is failing.

Here is my main Class :

import deliveryPayload.Payload;
import jakarta.ws.rs.*;

import jakarta.ws.rs.client.*;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import org.apache.commons.lang3.StringUtils;
import responsePayload.ResponsePayload;

import java.net.URI;
import java.util.*;


@Path("/hook")
public class Hook {

    private static final String apiToken = "myToken";
    private static final String domain = "url";

    private static final String apiUrl = "https://" + domain + "/api/v1/";

    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response eventHook(String body, @HeaderParam("Pass") String password) {
        ObjectMapper objectMapper = new ObjectMapper();
        Payload payload = new Payload();

        try {
            payload = objectMapper.readValue(body, Payload.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        EventsItem event = payload.getData().getEvents().get(0);

        Actor actor = event.getActor();

        Response response = ClientBuilder.newClient()
                .target(getBaseURI())
                .path("apps/" + "someID" + "/users")
                .request(MediaType.APPLICATION_JSON)
                .header(HttpHeaders.AUTHORIZATION, apiToken)
                .post(Entity.entity(actor, MediaType.APPLICATION_JSON));

        return response;
    }
}

I'm getting this error Parse Error: The response headers can't include "Content-Length" with chunked encoding when using Postman.

Thanks for any help !

Hekmil
  • 39
  • 7
  • [Read this first](https://stackoverflow.com/a/36949363/2587435). Then you should understand that you cannot return the client side Response as the server side Response. You need to extract what you need and recreate an outbound response. – Paul Samsotha Oct 06 '20 at 14:28
  • Removing the return statement and setting the function to void isn't correct ? I might need a code sample to understand – Hekmil Oct 06 '20 at 14:40
  • Did you read the link. You need to understand that there are two types of `Response`s. One is on the client side and one on the server side. You cannot treat the client side one like the server side one like you are trying to do. If you want to return on the server the response from the client, then you need to extract that data from the client Response first, and then return _that_ as the entity of the server response. – Paul Samsotha Oct 06 '20 at 14:44
  • Sorry I didn't see your link in your first response ! Well if I understand what you are saying once I finished serialize the data from the request I have to make another method to send it back ? But how can I extract it because I instantiate it in that first method. And second question, I thought the line `invocationBuilder.post(Entity.entity(actor, MediaType.APPLICATION_JSON));` was the one directly calling the API and didn't need returning a Response – Hekmil Oct 08 '20 at 07:23
  • `response.readEntity()`. That's how you extract the body from the client request. You need to pass a class to the method. – Paul Samsotha Oct 08 '20 at 07:25
  • What do you want to return to the caller of the eventHook endpoint? Do you want to return something from the apps/id/users client response? – Paul Samsotha Oct 08 '20 at 08:38
  • Ok so that's what I thought, I do not want to process the data posted. My code is supposed to stop at the post no response is waited necessary here. But of course if I wanted to return something from the post request to my API (error code or something) yeah I'd use `.readEntity()` but that's not the case here EDIT : just seen your message, but no that call API just send data to apps/id/users that's all – Hekmil Oct 08 '20 at 08:39
  • Ok so if you don't want to return anyting, you can make the eventHook method return void. But that would give you a 204 No Content response. Is that what you really want? If you want something else then you can use the Response class and one of its static methods to return a different status code. [See the docs](https://docs.jboss.org/resteasy/docs/2.0.0.GA/javadocs/javax/ws/rs/core/Response.html) for what methods you have available. – Paul Samsotha Oct 08 '20 at 08:42
  • Yeah that's what I planned to do, just wanted to make it functional first. But I'm getting a error 500 and this I don't know why, I'll have to investigate furthermore – Hekmil Oct 08 '20 at 08:55

0 Answers0