0

I have a JSON file that I want to upload to Server through a Multipart request. In this process, I need to determine the MIME type of the file. I have used the following method to determine the MIME type:

MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                    fileExtension.toLowerCase());

For an extension of ".json", it returns a null, i.e, json is not registered with any application type.

How can I get the MIME type of a JSON file? Above API is in the most upvoted answer on another SO post to get the MIME type of a file. Is there any API to obtain the same correctly?

Manish Kumar Sharma
  • 11,112
  • 7
  • 50
  • 86

2 Answers2

1

This might be too late for you, but perhaps for future reference:

The MIME media type of JSON is application/json, as mentioned in this very popular answer.

Since you know and expect your files to be .json, you can hardcode the MIME type as `application/json.

An example in Kotlin from the Android docs:

private val READ_REQUEST_CODE: Int = 42

private fun performFileSearch() {
    val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)

        type = "application/json" // MIME media type
    }

    startActivityForResult(intent, READ_REQUEST_CODE)
}
0

If you have Apache make sure this line is in .htaccess.

AddType application/json .json

If IIS make sure that the MIME type for *.json is application/json.