【问题标题】:How do I set the ContentType in a ServiceStack client?如何在 ServiceStack 客户端中设置 ContentType?
【发布时间】:2013-04-14 13:48:21
【问题描述】:

我正在使用 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 标头?

【问题讨论】:

    标签: c# forms ormlite-servicestack


    【解决方案1】:

    不要使用Servicestack's C# Clients 调用第 3 方 API。您正在使用 JSON 客户端,它按预期发送 JSON。如果需要调用 3rd Party 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>();
    

    【讨论】:

    • 谢谢mythz,这是有道理的。顺便说一句,url 参数已重复(参数列表中不需要),但由于编辑在 6 个字符以下,所以不会让我修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多