简体   繁体   中英

How do I set the ContentType in a ServiceStack client?

I am using a ServiceStack client to call a webservice as follows:

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

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

This generally works well, however I need to change the Content-Type header. By default (and for most other calls I am making from the service) this needs to be application/json , but in this particular case it needs to be application/x-www-form-urlencoded .

client.ContentType does not implement a setter, so how can I change the Content-Type header?

Don't use Servicestack's C# Clients to call 3rd party API's. You're using a JSON client which as expected sends JSON. You can use ServiceStack's built-in HTTP Utils if you need to call 3rd Party APIs, look at the POSTing data examples , eg:

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

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