Search code examples
c#asp.net-web-api2content-type

What's the Content-Type if my ASP.NET Web API returns a GUID?


I've got a simple method that validates credentials and returns a GUID.

I've got the return type as ActionResult<Guid> but when I call it from the auto-generated Swagger page, it returns a 406. I can change the return type to ActionResult<string> and pass back sessionGuid.ToString() and it works fine, but from the point of view of an user of the API, it seems as though it's more informative to specify the exact return type. The generated swagger.json recognises it, for example:

"content": {
    "text/plain": {
        "schema": {
            "type": "string",
            "format": "uuid"
        }
    }
}

and the Swagger contains a nice sample GUID instead of the generic word "string"

Swagger return type example

There doesn't seem to be a MIME type for a GUID. Is there a way of getting it to work?

[Consumes("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[Produces("text/plain")]
[HttpPost]
public ActionResult<Guid> Authenticate([FromBody] Credentials credentials) {
  Guid sessionGuid;

  try {
    sessionGuid = _database.CreateApiSession(credentials);
  } catch (BaseRecordNotFoundException) {
    return Unauthorized();
  } catch (BaseInvalidDataException) {
    return Unauthorized();
  }

  return Ok(sessionGuid);
}

Solution

  • We don't have MIME types for every data type possible.

    Your confusion arose from Swagger generating this information. But this is coming from Swagger scanning your actual code - that is why it's so good at constructing such suggestions/tips, etc. That's why it is great at.

    But it does not reflect MIME types that could be used.

    And from API user perspective - that is exactly why we have Swagger (or similair tools) - to be able to precisely tell what is the shape of the API, what are the definitions, etc.