I am trying to build a simple ping function to a mobile app. I want to send a ping from the app to an azure web app which should receive the ping and save it to an SQL database. I am developing the app in java and kotlin.
So far I have identified that the json payload I send from the app is not formatted correctly, or at least cannot be formatted correctly when received in the web app. I get a ‘Json input formatter’ exception from Azure Log Streams when I send a ping from the app.
I have tried to verify if my api was faulty be testing with reqbin.com, but sending the exact same json payload I do not get an error, and I cannot figure out why it works from reqbin but not from the app when the json is identical.
Any help would be greatly appreciated.
The class i use for constructing the json payload to send is in kotlin, like this:
*am importing all the stuff
fun createJsonPing(
timeOfPing: LocalDateTime,
deviceId: String,
smartphoneId: String,
note: String
): String {
val messageObject = JSONObject()
try {
// convert the datetime to correct format
val isoDateTimeString = timeOfPing.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
messageObject.put("TimeOfPing", formatDateTimeToISO(timeOfPing))
messageObject.put("SmartphoneId", smartphoneId)
messageObject.put("DeviceId", deviceId)
messageObject.put("Note", note)
} catch (e: Exception) {
e.printStackTrace()
throw JSONException(e.message ?: "Unknown error")
Log.i("createJsonPing", "Failed to construct Json ping")
}
val jsonMessageStringPing = messageObject.toString()
Log.i("createPing", "Ping: n" + jsonMessageStringPing)
Log.i("createPing", "Size of ping is: ${jsonMessageStringPing.toByteArray(Charsets.UTF_8).size} b")
return jsonMessageStringPing
}
fun formatDateTimeToISO(dateTime: LocalDateTime): String {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
return dateTime.format(formatter)
}
I then send the json using Ktor like this:
// set up client
private val client = HttpClient(CIO) {
install(Logging) {
level = LogLevel.ALL
}
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
prettyPrint = true
//isLenient = true
})
}
}
suspend fun simpleSend() {
try {
val response: HttpResponse = client.post("https://myazurewebapp.azurewebsites.net/ping") {
headers {
append(HttpHeaders.Accept, "application/json")
append(HttpHeaders.ContentType, "application/json")
}
//contentType(ContentType.Application.Json) // <--have also trying using this
setBody(createJsonPing(
LocalDateTime.now(),
"TEST",
"test",
"testing ping"))
}.body()
Log.i(TAG, "HTTP Response: $response")
} catch (e: Exception) {
Log.i(TAG, "Request failed: $e")
} finally {
close()
}
}
The format of the json i send looks like this:
{"TimeOfPing":"2024-06-21T14:38:00.121",
"SmartphoneId":"TEST",
"DeviceId":"test",
"Note":"testing ping"}
And i get an error from Azure Log Streams in my web app like this:
2024-06-21 12:20:50.894 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1 POST https://myazurewepapp.azurewebsites.net/ping - application/json 127
2024-06-21 12:20:50.894 +00:00 [Trace] Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware: All hosts are allowed.
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Routing.Matching.DfaMatcher: 1 candidate(s) found for the request path '/ping'
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Endpoint 'api.Controllers.MessagePingController.Create (api)' with route pattern 'api/ping' is valid for the request path '/ping'
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: Request matched endpoint 'api.Controllers.MessagePingController.Create (api)'
2024-06-21 12:20:50.894 +00:00 [Trace] Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: The endpoint does not specify the IRequestSizeLimitMetadata.
2024-06-21 12:20:50.894 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executing endpoint 'api.Controllers.MessagePingController.Create (api)'
2024-06-21 12:20:50.894 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Route matched with {action = "Create", controller = "MessagePing"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Create(api.Dtos.MessagePing.CreateMessagePingRequestDto) on controller api.Controllers.MessagePingController (api).
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Execution plan of authorization filters (in the following order): None
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Execution plan of resource filters (in the following order): None
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Execution plan of action filters (in the following order): Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter (Order: -3000), Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter (Order: -2000)
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Execution plan of exception filters (in the following order): None
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Execution plan of result filters (in the following order): Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter (Order: -2000)
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executing controller factory for controller api.Controllers.MessagePingController (api)
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed controller factory for controller api.Controllers.MessagePingController (api)
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Attempting to bind parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto' ...
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder: Attempting to bind parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto' using the name '' in request data ...
2024-06-21 12:20:50.894 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder: Selected input formatter 'Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter' for content type 'application/json'.
----
2024-06-21 12:20:50.901 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter: JSON input formatter threw an exception: The JSON value could not be converted to api.Dtos.MessagePing.CreateMessagePingRequestDto. Path: $ | LineNumber: 0 | BytePositionInLine: 127.
----
2024-06-21 12:20:50.902 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder: Done attempting to bind parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto'.
2024-06-21 12:20:50.903 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Done attempting to bind parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto'.
2024-06-21 12:20:50.903 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Attempting to validate the bound parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto' ...
2024-06-21 12:20:50.903 +00:00 [Debug] Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Done attempting to validate the bound parameter 'MessagePingDto' of type 'api.Dtos.MessagePing.CreateMessagePingRequestDto'.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
2024-06-21 12:20:50.905 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter: The request has model state errors, returning an error response.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
2024-06-21 12:20:50.905 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter'.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: Before executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
2024-06-21 12:20:50.905 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Action Filter: After executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Before executing action result Microsoft.AspNetCore.Mvc.BadRequestObjectResult.
2024-06-21 12:20:50.906 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector: List of registered output formatters, in the following order: Microsoft.AspNetCore.Mvc.Formatters.HttpNoContentOutputFormatter, Microsoft.AspNetCore.Mvc.Formatters.StringOutputFormatter, Microsoft.AspNetCore.Mvc.Formatters.StreamOutputFormatter, Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter
2024-06-21 12:20:50.906 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector: Attempting to select an output formatter based on Accept header 'application/json, application/json' and explicitly specified content types 'Microsoft.AspNetCore.Mvc.Formatters.MediaTypeCollection'. The content types in the accept header must be a subset of the explicitly set content types.
2024-06-21 12:20:50.906 +00:00 [Debug] Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector: Selected output formatter 'Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter' and content type 'application/problem+json' to write the response.
2024-06-21 12:20:50.906 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Executing BadRequestObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails'.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: After executing action result Microsoft.AspNetCore.Mvc.BadRequestObjectResult.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
2024-06-21 12:20:50.906 +00:00 [Trace] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
2024-06-21 12:20:50.907 +00:00 [Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Executed action api.Controllers.MessagePingController.Create (api) in 12.6443ms
2024-06-21 12:20:50.907 +00:00 [Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executed endpoint 'api.Controllers.MessagePingController.Create (api)'
2024-06-21 12:20:50.909 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished HTTP/1.1 POST https://myazurewepapp.azurewebsites.net/ping - 400 - application/problem+json;+charset=utf-8 16.0021ms
I have marked the line I think is causing me trouble with —-
As I can understand the json I send from the app is somehow not formatted correctly so that the api can handle/format it to further process.
I have however tryed to test my api with reqbin.com, where I can succesfully send the exact same json which I send from the app. And I cannot figure out why not.
When i POST from reqbin the json is accepted and put into my sql database with out any trouble.
The GET api endpoint I have to receive the ping is like this:
{
"type": "object",
"properties": {
"TimeOfPing": {
"type": "string"
},
"SmartphoneId": {
"type": "string"
},
"DeviceId": {
"type": "string"
},
"ProcessNote": {
"type": "string"
}
},
"additionalProperties": false
}
I cannot identify any problems there.