![](/img/trans.png)
[英]How to set default content-type to Json with servicestack serializer
[英]Is it possible or necessary to set the content type of a ServiceStack client delete request?
我有以下代碼。
public T SendUpdateRequest(string url)
{
using (JsonServiceClient client = new JsonServiceClient())
{
T response = client.Put<T>(url);
return response;
}
}
我有類似的創建和刪除請求方法,分別調用JsonServiceClient Post
和Delete
方法。
調用我的更新或創建方法時,對外部API的調用工作正常。 刪除沒有。 我可以看到,如果我通過REST控制台向它發出請求,那么API的delete方法確實有效。
當我將我的非工作刪除與Fiddler中的工作請求/響應進行比較時,我可以看到主要的區別是我的請求是沒有將content-type
設置為application/json
(所有這些方法都返回JSON)。
我的問題是,為了成功調用我的API方法,是否有可能(甚至有必要)將我的刪除請求的content-type
顯式設置為application/json
?
ServiceStack客戶端不在沒有請求主體的請求上設置content-type
標頭,因為content-type
僅適用於主體,因此是多余的。
if (httpMethod.HasRequestBody())
{
client.ContentType = ContentType;
...
正確實現的RESTful服務應該滿足於DELETE
請求,而沒有在沒有正文的情況下指定content-type
。
DELETE /User/123 HTTP/1.1
如果您正在調用的服務對您的請求不滿意而未指定此類型(這是不常見的) ,那么您可以使用此過濾器手動強制發送類型:
var client = new JsonServiceClient("https://service/");
client.RequestFilter += (httpReq) => {
// Force content type to be sent on all requests
httpReq.ContentType = "application/json";
};
我希望有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.