2

I recieve error from outer api, I think there is something wrong with my generated json content because when I'm sending the same data from postman, I get correct response.

Is there a way to preview what json will be generated from JsonContent? I have a http client method:

public async Task<ResponseDto> UploadAsync(RequestDto model, CancellationToken cancellationToken)
{
    var requestUri = HttpHelper.BuildUri(uri.UPLOAD);    
    JsonContent content = JsonContent.Create(model);  //<= how read that JsonContent   
    /* there is not problems
    var response = await _client.PostAsync(requestUri, content, cancellationToken);

    string responseContent = await response.Content.ReadAsStringAsync(cancellationToken);

    return JsonSerializer.Deserialize<ResponseDto>(responseContent)
        ?? throw new HttpRequestException(responseContent);*/
}
2
  • I'd expect you'd need to call .SerializeToStream to a MemoryStream and then convert that to a string. Or use a tool like Telerik's Fiddler to capture the request. I looks like System.Text.Json is used internally, so perhaps try just serializing it with that to start with to see if anything obvious if wrong. Commented Dec 19, 2022 at 5:50
  • 1
    Alternatively you could serialize your object to a string, log that, then use StringContent to post it as explained here. P.S alternatively to Fiddler you could also use Wireshark since it's free. Commented Dec 19, 2022 at 7:15

1 Answer 1

4

var contentAsString = await content.ReadAsStringAsync();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.