set default content-type

Hi,

When using the 'Extract Variables' policy to extract a JSON payload, it wont extract any variables in case the Content-Type is not set. While I understand why, I have two questions;

  1. Is it a good practice to set a default value in the API for the Content-Type in case the header was not supplied? Or should I send a HTTP 400?
  2. Can I expect that most clients actually send the correct Content-Type header? For example, Postman doesnt by default.

thanks and kind regards,

Guy

1 1 1,026
1 REPLY 1

Dear @GuyH ,

Great Questions. Please find my opinion on same,

1. Is it a good practice to set a default value in the API for the Content-Type in case the header was not supplied? Or should I send a HTTP 400?

It depends on your API implementation. If you know the content type expected by your API End Point then it's always good practice to set same in your API Proxy if not received from client. So that, even if client fails to send same you can handle it in the API proxy. I believe overriding in the API proxy is a bad practice if client sends same.

Most of the times, same API accepts multiple content types, based on that your response will be calculated. In that case it's not ideal to set a default one in the Proxy.

2 . Can I expect that most clients actually send the correct Content-Type header? For example, Postman doesn't by default.

Good Point, Thats where API Documentation plays a key role. Many of them give lower priority to the API Documentation than implementing APIs or building features around APIs. API Documentation is the entry point to your services which are consumed by APIs. For a developer every service that you implement in server is like a black box, only thing that helps a developer to consumer APIs is API Documentation. Make sure you document expected content-type headers in API Documentation and include error codes related to missing content types.

Hope it helps.

Cheers,

Anil Sagar