POST with JSON corpus consumed by RESTeasy - HTTP 100

I have a very simple scenario that I cannot work with. I am trying to POST a JSON string to a RESTful endoint using cURL to send a request over HTTPS and RESTeasy to the server.

My cURL POST is configured like this:

$ch = curl_init();

$content = json_encode($validJsonString);

curl_setopt($ch, CURLOPT_URL, 'https://foobar.com/test?trackingId=12345');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $content));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

$headers = array( 
    'Content-Type: application/json',
    'Content-Length: ' . strlen($content)
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec ($ch);

$httpResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$json = json_decode($response);
curl_close ($ch);

return $json;

      

And the RESTeasy receiver looks like this:

@POST
@Path("/test")
@Produces("application/json")
public String addObjectCommentAsJSON(   @FormParam("json") String validJsonString, 
                                        @QueryParam("trackingId") String trackingId) {
    Gson gson = new Gson();
    SomeObject someObject = gson.fromJson(validJsonString, SomeObject.class);
    String responseString = methodThatReturnsAJsonString(trackingId, someObject);
    return responseString;
}

      

This issue is because the request hangs for about 30 seconds and then returns an HTTP 100 response. I understand that HTTP 100 means continuing the rest of the request, but I don't understand how I should do it.

I tried to remove the Content-Length header but obviously I get 411 (content length required) so not an option.

Possibly a content type issue? Any help is greatly appreciated.

+3


source to share


1 answer


Ok, so thanks to suggestions by @cbuckley and @abraham in their comments, I was able to resolve the issues. This is what I needed to do:

1-> Change the content of the POST body and instead of curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $content));

now I havecurl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($validJsonString));

2-> I created a custom @Provider

one that parses a JSON string into a Java POJO with the same structure:

@Provider
public class SomeObjectProvider implements StringConverter<SomeObject> {

    private final Gson gson = new Gson();
    public SomeObject fromString(String s) {
        return gson.fromJson(s, SomeObject.class);
    }

    public String toString(SomeObject someObject) {
        return gson.toJson(SomeObject);
    } 
}

      



3-> Finally, the structure of the getter method should change slightly:

@POST
@Path("/test")
@Produces("application/json")
public String addObjectCommentAsJSON(SomeObject someObject,
                                     @QueryParam("trackingId") String trackingId) {
    String responseString = methodThatReturnsAJsonString(trackingId, someObject);
    return responseString;
}

      

And it all went well.

+2


source







All Articles