Search code examples
c#gethttpclient

How to include \r\n when trying to do a Get request?


One of the servers that I'm trying to request on have \r\n in the string. These new lines are clearly visible when you try to get the request via the browser or tools such as Postman. However when I do try to get the string programatically by code, I don't see the \r\n. Why is this or is it even possible to retrieve the \r\n?

public async Task<TResponse> GetAsync(string uri)
{
    var httpClient = new HttpClient();
    var content = await httpClient.GetStringAsync(uri);
    return JsonConvert.DeserializeObject<TResponse>(content);
}

Response should look like this in which the formatting is needed to be preserved: enter image description here


Solution

  • Found a way to preserve the carriage returns by using HttpUtility.JavaScriptStringEncode to the response that I receive.

    https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility.javascriptstringencode?view=netcore-3.1