Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Frends.HTTP.Request/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.9.0] - 2026-04-15

### Fixed

- Fixed an issue where selecting JToken as the return format would throw an exception if the server returned a non-JSON response; the response body is now returned as a raw string instead, preserving the status code for flow control.

## [1.8.0] - 2026-03-07

### Fixed
Expand Down
57 changes: 51 additions & 6 deletions Frends.HTTP.Request/Frends.HTTP.Request.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public async Task RestRequestShouldNotThrowIfReturnIsEmpty()
}

[TestMethod]
public void RestRequestShouldThrowIfReturnIsNotValidJson()
public async Task RestRequest_NonJsonResponse_ShouldReturnRawStringInsteadOfThrowing()
{
var input = new Input
{
Expand All @@ -285,12 +285,11 @@ public void RestRequestShouldThrowIfReturnIsNotValidJson()
Token = "fooToken"
};

// _mockHttpMessageHandler.When(input.Url)
// .Respond("application/json", "<fail>failbar<fail>");
var ex = Assert.ThrowsAsync<JsonReaderException>(async () =>
await HTTP.Request(input, options, CancellationToken.None));
var result = await HTTP.Request(input, options, CancellationToken.None);

Assert.That(ex.Message.Contains("Unable to read response message as json"));
ClassicAssert.IsInstanceOf<string>(result.Body);
ClassicAssert.IsTrue(((string)result.Body).Contains("<!--"));
ClassicAssert.AreEqual(200, result.StatusCode);
}

[TestMethod]
Expand Down Expand Up @@ -411,4 +410,50 @@ public void CorrectStoreSearched(CertificateStoreLocation storeLocation, string
Assert.That(ex.Message.Contains(
$"Certificate with thumbprint: 'INVALIDTHUMBPRINT' not found in {storeLocationText} cert store."));
}

[TestMethod]
public async Task RestRequest_HtmlContentType_ShouldReturnRawStringInsteadOfThrowing()
{
var input = new Input
{
Method = Method.Method.GET,
Url = $"{BasePath}/html",
Headers = new Header[0],
Message = "",
ResultMethod = ReturnFormat.JToken
};
var options = new Options
{
ConnectionTimeoutSeconds = 60,
ThrowExceptionOnErrorResponse = false
};

var result = await HTTP.Request(input, options, CancellationToken.None);

ClassicAssert.AreEqual(200, result.StatusCode);
ClassicAssert.IsInstanceOf<string>(result.Body);
ClassicAssert.IsTrue(((string)result.Body).Contains("<html"));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

[TestMethod]
public async Task RestRequest_JsonContentType_ShouldStillReturnJToken()
{
var input = new Input
{
Method = Method.Method.GET,
Url = $"{BasePath}/json",
Headers = new Header[0],
Message = "",
ResultMethod = ReturnFormat.JToken
};
var options = new Options
{
ConnectionTimeoutSeconds = 60
};

var result = await HTTP.Request(input, options, CancellationToken.None);

ClassicAssert.IsInstanceOf<JToken>(result.Body);
ClassicAssert.AreEqual(200, result.StatusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>1.8.0</Version>
<Version>1.9.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
15 changes: 10 additions & 5 deletions Frends.HTTP.Request/Frends.HTTP.Request/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ CancellationToken cancellationToken

break;
case ReturnFormat.JToken:
var rbody = TryParseRequestStringResultAsJToken(await responseMessage.Content
var rawBody = await responseMessage.Content
.ReadAsStringAsync(cancellationToken)
.ConfigureAwait(false));
.ConfigureAwait(false);

var rbody = TryParseBody(rawBody);
var rstatusCode = (int)responseMessage.StatusCode;
var rheaders =
GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers);
Expand Down Expand Up @@ -205,15 +207,18 @@ private static HttpContent GetContent(Input input, IDictionary<string, string> h
return new StringContent(input.Message ?? string.Empty);
}

private static object TryParseRequestStringResultAsJToken(string response)
private static object TryParseBody(string responseBody)
{
if (string.IsNullOrWhiteSpace(responseBody))
return new JValue("");

try
{
return string.IsNullOrWhiteSpace(response) ? new JValue("") : JToken.Parse(response);
return JToken.Parse(responseBody);
}
catch (JsonReaderException)
{
throw new JsonReaderException($"Unable to read response message as json: {response}");
return responseBody;
}
}

Expand Down
Loading