简体   繁体   中英

Is it possible or necessary to set the content type of a ServiceStack client delete request?

I have the following code.

public T SendUpdateRequest(string url)
{
    using (JsonServiceClient client = new JsonServiceClient())
    {
        T response = client.Put<T>(url);
        return response;
    }
}

I have similar methods for create and delete requests, calling the JsonServiceClient Post and Delete methods respectively.

When calling my update or create methods, the call to the external API works fine. Delete does not. I can see that the API's delete method does indeed work if I fire a request to it via REST console .

When I compare my non-working delete with the working one's request/response in Fiddler, I can see the main difference is my request is not setting content-type to application/json (all these methods return JSON).

My question is, is it possible (or even necessary) to explicitly set the content-type of my delete request to application/json in order to successfully call my API method?

The ServiceStack clients do not set the content-type header on requests where there is no request body, as the content-type only applies to the body, and is therefore redundant.

This can be seen here in the code that prepares the client request.

if (httpMethod.HasRequestBody())
{
    client.ContentType = ContentType;
    ...

A correctly implemented RESTful service should be happy with a DELETE request without content-type being specified where there is no body.

DELETE /User/123 HTTP/1.1

If the service you are calling is not happy with your request without this type being specified (which is unusual) , then you can manually enforce the sending of the type using this filter:

var client = new JsonServiceClient("https://service/");
client.RequestFilter += (httpReq) => {
    // Force content type to be sent on all requests
    httpReq.ContentType = "application/json";
};

I hope that helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM