2013-04-14 23 views
2

我使用的是ServiceStack客户端调用web服务如下:如何在ServiceStack客户端中设置ContentType?

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

请勿使用Servicestack's C# Clients来调用第三方API。您正在使用预期发送JSON的JSON客户端。

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

感谢mythz,这是有道理的:如果你需要调用第三方的API,看看POSTing data examples,e.g您可以使用ServiceStack's built-in HTTP Utils。顺便说一句,url参数已被复制(参数列表中不需要),但由于编辑是在6个字符以下,SO不会让我修复它。 –

+0

@DarrenOster酷固定,thx。 – mythz

相关问题