Getting content/message from HttpResponseMessage

后端 未结 8 2210
抹茶落季
抹茶落季 2020-12-01 00:18

I\'m trying to get content of HttpResponseMessage. It should be: {\"message\":\"Action \'\' does not exist!\",\"success\":false}, but I don\'t know, how to get

8条回答
  •  失恋的感觉
    2020-12-01 01:03

    If you want to cast it to specific type (e.g. within tests) you can use ReadAsAsync extension method:

    object yourTypeInstance = await response.Content.ReadAsAsync(typeof(YourType));
    

    or following for synchronous code:

    object yourTypeInstance = response.Content.ReadAsAsync(typeof(YourType)).Result;
    

    Update: there is also generic option of ReadAsAsync<> which returns specific type instance instead of object-declared one:

    YourType yourTypeInstance = await response.Content.ReadAsAsync();
    

提交回复
热议问题