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

本文关键字:设置 ContentType 客户端 ServiceStack | 更新日期: 2023-09-27 18:09:00

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

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

不要使用Servicestack的c#客户端调用第三方API。您正在使用一个JSON客户端,如预期的那样发送JSON。如果你需要调用第三方api,你可以使用ServiceStack的内置HTTP Utils,查看post数据示例,例如:

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