[英]How to add 'version' field to Content-Type Header in VB.NET
I'm implementing communication with an API REST for my Visual Basic.Net application.我正在为我的 Visual Basic.Net 应用程序实现与 API REST 的通信。 The issue appears when I try to add the field
version=1
to the Content-Type
header.当我尝试将字段
version=1
添加到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
我已经与 API 的开发人员进行了交谈,他们说我必须在 Content-Type header 上包含版本字段,这就是他们所说的: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:我通过分别声明
application/vnd.gestiona.third+json
内容类型和version=1
类型来解决它,如下所示:
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.这样,它就像一个魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.