deserialize individual response of an HTTP batch request to it's suitable data type. RRS feed

All replies

  • User-474980206 posted

    you need a custom MediaTypeFormatter that supports "multipart/mixed". check this thread:


    Sunday, July 15, 2018 3:23 AM
  • User-803397181 posted

    it <g class="gr_ gr_29 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="29" data-gr-id="29">throw</g> <g class="gr_ gr_28 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="28" data-gr-id="28">exception</g>:

    No MediaTypeFormatter is available to read an object of type 'ResponseModel' from content with media type 'application/<g class="gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="13" data-gr-id="13">http</g>'.

    When <g class="gr_ gr_27 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="27" data-gr-id="27">i</g> call this:

    var p = queryResponse.ReadAsAsync<ResponseModel>(new[] { new JsonMediaTypeFormatter() }).Result;

    the response text is like this:

    HTTP/1.1 200 OK
    Content-Type: application/json; charset=utf-8


    Sunday, July 15, 2018 5:15 AM
  • User-474980206 posted

    the JsonMediaFormater will not work, because the media is multipart/mixed. as I said, you need to write a custom MediaFormatter that handles the multipart/mixed. The only supplied multipart formatter is the multipart/form-data, which has files and url-encoded form data. 

    if the content parts are all json, if you want types objects, you will need some way to determine the type to deserialize. if they are all the same, then you format can be a generic that return a typed collection of objects.  

    var result =  await queryResponse.ReadAsAsync<IEnumerable<ResponseModel>>(new[] { new MyMediaTypeFormatter<ResponseModel>() });

    Sunday, July 15, 2018 11:42 PM