我尝试在Domino服务器上使用REST Api,如下所述:Document patch
当我通过Postman发送请求时,一切正常,但当我尝试通过RestSharp以编程方式提出相同请求时,错误415 Unsupported Media Type
。
这是我用来发送请求的 vb.net 代码:
Function Execute(request As RestRequest) As RestResponse
Dim client As New RestClient(baseUrl)
request.AddHeader("Content-Type", "application/json")
request.AddHeader("Accept", "application/json")
Dim response = client.Execute(request)
If response.StatusCode <> 200 Then
Throw New ApplicationException(response.StatusDescription)
End If
If response.ErrorException IsNot Nothing Then
Throw New ApplicationException("Error retrieving response. Check inner details for more info.", response.ErrorException)
End If
Return response
End Function
Public Sub SetState(DocUNID As String, state As String)
Dim request = New RestRequest()
request.Method = Method.PATCH
request.Resource = "api/data/documents/unid/{DocUNID}"
request.AddParameter("EMPCARDSTATE", state)
Execute(request)
End Sub
这是我的 Postman 屏幕截图:
答案 0 :(得分:0)
发送PATCH请求时,数据服务返回415,而Content-Type标头值不是 application / json。我不熟悉vb.net,但我只能断定这句话没有按预期工作:
request.AddHeader("Content-Type", "application/json")
这句话对我来说也是错误的:
request.AddParameter("EMPCARDSTATE", state)
您希望将JSON对象写入请求正文。我想你打算在JSON对象中包含一个“EMPCARDSTATE”属性。 request.AddParameter()
是否真的设置了请求的主体?
我建议您更改代码以正确设置请求正文。这可能是解决问题所需的全部内容。如果您仍然看到415错误,您可以使用Fiddler(或类似工具)来检查Content-Type标头和正文的请求。
答案 1 :(得分:0)
看起来问题出在POST / PATCH vs GET的RestSharp实现上。当我在url中发送带有参数模板的POST / PATCH时跟随def default_error_handler(exc, batch):
"""
Default callback function that is triggered when the cassandra async operation failed
:param exception:
"""
logging.error("Query '%s' failed: %s", batch, exc)
,它抛出异常。如果我在使用它之前连接url,一切正常。