1

Hi I have a json request which I have to pass in my junit testcase but the request is delete as delete do not support setEntity method. How can I pass the request in my testcase. Json request which I have to pass

{

"userId":"AJudd",
"siteId":"131",
"alternateSiteId":"186"

}

mytest case for this

@Test
    public void testdeleteAltSite() throws ClientProtocolException, IOException {
        String resultCode = "001";
        String resultText = "Success";
        String url = "http://localhost:8080/adminrest1/alternatesite";
        HttpClient client = HttpClientBuilder.create().build();
        HttpDelete delete = new HttpDelete(url);

        // add header
        delete.addHeader("Transaction-Id", "11");
        delete.addHeader("content-type", "application/json");
        LOG.info(url);
        HttpResponse response = client.execute(delete);

        byte[] buf = new byte[512];
        InputStream is = response.getEntity().getContent();
        int count = 0;
        StringBuilder builder = new StringBuilder(1024);
        while ((count = is.read(buf, 0, 512)) > 0) {
            builder.append(new String(buf, 0, count));
        }
        String output = builder.toString();
        System.out.println(output);

    }`

How to pass the json value so that the passed value data can be deleted?

2 Answers2

0

Ok, first of all: Sending a body with a DELETE is not what usually happens around the internet. Nevertheless, it is not forbidden (Is an entity body allowed for an HTTP DELETE request?). So, two ideas:

1) New class

I assume you use org.apache.http.client: Just extend HttpEntityEnclosingRequestBase:

public class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase {

    public final static String METHOD_NAME = "DELETE";

    public HttpDeleteWithEntity() {
        super();
    }

    public HttpDeleteWithEntity(final URI uri) {
        super();
        setURI(uri);
    }

    public HttpDeleteWithEntity(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }

}

This is basically c&p'ed from the HttpPost class. I did not test this, tho.

Then use your HttpDeleteWithEntity class instead of HttpDelete.

2) Use custom headers

If you can modify your server code that might be a good alternative.

delete.addHeader("testwith", jsonString);

or

delete.addHeader("userId","AJudd");
delete.addHeader("siteId","131");
delete.addHeader("alternateSiteId","186);

Finally, if you are in charge of the server implementation I would recommend to implement DELETE requests without any body (see artemisian's answer).

Community
  • 1
  • 1
nCessity
  • 735
  • 7
  • 23
0

IMHO this is a problem with your design.

If your intent is to delete an alternate site and its id is unique then passing the alternateSiteId as part of the URI should sufficient:

Method: DELETE
URL: http://localhost:8080/adminrest1/alternatesite/{alternateSiteId}

If alternateSiteId is not unique then you are updating a relationship. In that case you should use a PUT which allows you to include a body in your request. Please note you should pass the id of the resource you are updating as part of your URI, for example:

Method: PUT
URL: http://localhost:8080/adminrest1/alternatesite/{userId}
Body:{ 
       "siteId":"131",
       "alternateSiteId":"186"
}
artemisian
  • 2,976
  • 1
  • 22
  • 23