ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,494 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm transforming a vb.net class into a JsonSerializer.Serialize and after trying to integrate with an APi it presents an error not finding the client.
I did a test and went straight to HttpRequestMessage
request.Content = New StringContent("{""contractor"":{""numero"":""015380"",""tipo"":2}") with two quotes and it worked, but with just one quote like serialized does not find the contrinuent.
Any suggestion?
Thank you very much in advance.
The problem might be due to the way you’re serializing your data. When you manually create a JSON string with double quotes ("{""contractor"":{""numero"":""015380"",""tipo"":2}"), it works fine. However, when you use JsonSerializer.Serialize, which uses single quotes, it doesn’t work.
Try with single quotes.
Dim NovasMensagens As New EntradaIndicadorNovasMensagens()
NovasMensagens.contratante = New Contratante() With {
.numero = 123456789,
.tipo = 2
}
Dim jsonString As String = JsonSerializer.Serialize(NovasMensagens)
Using httpClient = New HttpClient()
Using request As New HttpRequestMessage(HttpMethod.Post, URLBASE & "Monitorar")
request.Headers.TryAddWithoutValidation("accept", "*/*")
request.Headers.TryAddWithoutValidation("jwt_token", JWK_Token)
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " & Acesss_Token)
request.Headers.TryAddWithoutValidation("Content-Type", "application/json")
Dim stringContent As New StringContent(jsonString, Encoding.UTF8, "application/json")
Dim response As Task(Of HttpResponseMessage) = httpClient.SendAsync(request)
Return Await response.Result.Content.ReadFromJsonAsync(Of RetornoIndicadorNovasMensagens)(OpcoesDesserializarJson)
End Using
End Using