3

I'm in the process of writing a testing framework for an application, and I am not allowed to update, delete, move, or basically do anything with the data used by this application. For GET requests I need to test this is no problem, but PUT, POST and DELETE methods that change data this obviously is not the case.

Is there any way to send a POST request without any body, and still get a response that shows the url can take a request? Or in other words, how can I show that a url that is a POST is up and able to take requests, without actually sending the POST request and changing something in the database? (unfortunately its not possible to add a test object to database and run requests on that).

I need to do this programmatically in either Java or C# as well.

  • Does this answer your question? [POST with curl without sending data](https://stackoverflow.com/questions/2885274/post-with-curl-without-sending-data) – g_bor Sep 14 '20 at 16:25
  • I would need this to be done programmatically in java or C#, not from command prompt. Thank you though @g_bor ! I might end up using that some other time – Wojtek Wojciechowski Sep 14 '20 at 16:31
  • Does this help? https://stackoverflow.com/questions/7907648/post-an-empty-body-to-rest-api-via-httpclient – g_bor Sep 14 '20 at 16:37
  • @g_bor unfortunately my application treats an empty body as null, so the field the POST is changing gets set to null, which I am not allowed to do – Wojtek Wojciechowski Sep 14 '20 at 16:54
  • According to the highly upvoted comment in the thread I posted setting the content to null will not send a body. Can you confirm that this is the case? – g_bor Sep 14 '20 at 17:02
  • @g_bor yes I did both a null value for HTTPContent, which caused a bad request error, as well as {}, which was treated as null and then {null} was added to the field the POST changes in the database. My application is designed to throw an error if the body is null, and an empty JSON object gets treated as null I believe – Wojtek Wojciechowski Sep 15 '20 at 17:26

1 Answers1

3

There is no general way to 'test' if a POST request will work.

Most servers will likely emit a 400 error for these endpoints, which doesn't tell you a lot.

The most standard way to see if something is able to accept a POST request at all, is probably by doing an OPTIONS request and using the Allow header in the response to get the list of supported methods.

There is no guarantee that this is going to be correct, but many modern frameworks do a decent job populating this list. This is likely going to give you the most accurate, but still imperfect results.

You should not send an empty POST request anywhere because it could have a meaning and you could make unexpected changes to a server. For this kind of introspection stuff, stick to the 'safe' methods.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Yeah sending an empty POST looks like it will be treated as null and add null values to the server, but the OPTIONS suggestion of yours I think should work for what I'm trying to do, thanks for your response! – Wojtek Wojciechowski Sep 15 '20 at 17:19