如何在ServiceStack客户端中设置ContentType?

时间:2013-04-14 13:48:22

标签: c# forms ormlite-servicestack

我使用ServiceStack客户端调用web服务,如下所示:

var client = new JsonServiceClient(apiUrl);
var url = "/V1/MyApiCall";

var response = client.Post<MyApiCallResponse>(url, "foo=" + request.foo + "&bar=" + request.bar);

这通常很有效,但我需要更改Content-Type标头。默认情况下(对于我从服务中进行的大多数其他调用),这必须是application/json,但在这种特殊情况下,它必须是application/x-www-form-urlencoded

client.ContentType未实现setter,因此如何更改Content-Type标头?

1 个答案:

答案 0 :(得分:1)

请勿使用Servicestack's C# Clients来呼叫第三方API。您使用的JSON客户端按预期发送JSON。如果您需要调用第三方API,可以使用ServiceStack's built-in HTTP Utils,查看POSTing data examples,例如:

var response = url.PostToUrl(new { foo = request.foo, bar = request.bar },
                   acceptContentType = "application/json")
    .FromJson<MyApiCallResponse>();