I'm implementing communication with an API REST for my Visual Basic.Net application. The issue appears when I try to add the field version=1
to the Content-Type
header. Here's the code I use in order to do it:
Public Function AddTercero(tercero As Tercero, connection As GestionaConnection) As Boolean
If connection Is Nothing Then
Throw New ArgumentNullException(NameOf(connection))
End If
If tercero Is Nothing Then
Throw New ArgumentNullException(NameOf(tercero))
End If
Dim request = New HttpRequestMessage()
request.RequestUri = New Uri(Convert.ToString(connection.RecursosDictionary("vnd.gestiona.thirds"), CultureInfo.CurrentCulture))
request.Method = HttpMethod.Post
request.Headers.Add("X-Gestiona-Access-Token", AccessToken)
request.Headers.Add("Accept", "application/json")
request.Content = New StringContent(JsonConvert.SerializeObject(tercero))
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json; version=1")
Dim req = request.Content.ReadAsStringAsync
Dim response As HttpResponseMessage = connection.Connection.SendAsync(request).Result
request.Dispose()
Dim resultado = response.Content.ReadAsStringAsync()
Debug.WriteLine(resultado.Result.ToString)
If response.StatusCode = HttpStatusCode.Created Then
Return True
ElseIf response.StatusCode = HttpStatusCode.Unauthorized Then
Throw New InvalidOperationException("Error al añadir tercero: no tiene autorización " & response.ReasonPhrase)
Else
Throw New InvalidOperationException("Error al añadir tercero: " & response.StatusCode & " -> " & response.ReasonPhrase)
End If
End Function
The error message says:
System.InvalidOperationException: Error al añadir tercero: 415 -> Unsupported Media Type
And the message I get from the server is:
{
"code": 415,
"name": "Unsupported Media Type",
"description": "Content not supported: application/vnd.gestiona.third+json",
"technical_details": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16"
}
I have spoken with the developers of the API and they say I must include the version field on the Content-Type header, here's what they said: Content-Type with version: application/vnd.gestiona.third+json; version=1
application/vnd.gestiona.third+json; version=1
Any ideas on how could I solve this problem?
Thank you for reading
I solved it by declaring the application/vnd.gestiona.third+json
content type and the version=1
type separately as it follows:
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json")
request.Content.Headers.ContentType.Parameters.Add(New NameValueHeaderValue("version", "1"))
Instead of doing it as I was:
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json; version=1")
This way, it works as a charm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.