Skip to content

Commit

Permalink
Fix API requests that rely on cookie authentication failing in 22.1.7…
Browse files Browse the repository at this point in the history
…4+ (Fix #268)
  • Loading branch information
lordmilko committed Feb 16, 2022
1 parent 6da5e66 commit c74bbf6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/PrtgAPI/Request/PrtgRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal class PrtgRequestMessage

public Uri Uri { get; }

public IDictionary<string, string> Headers { get; } = new Dictionary<string, string>();

/// <summary>
/// Gets a parser capable of modifying the request response. If a custom parser external to this request message
/// is specified for the API request, this parser is ignored.
Expand Down Expand Up @@ -77,6 +79,9 @@ private PrtgRequestMessage(ConnectionDetails connectionDetails, string function,
AddParameter(urlBuilder, Parameter.PassHash, connectionDetails.PassHash);
}

if (parameters.Cookie)
Headers.Add("X-Requested-With", "XMLHttpRequest");

Uri = new Uri(urlBuilder.ToString());

Parser = parameters as IResponseParser;
Expand Down
12 changes: 11 additions & 1 deletion src/PrtgAPI/Request/PrtgWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@ public Task<HttpResponseMessage> SendAsync(PrtgRequestMessage request, Cancellat
//needs to be refactored to have the deserialization happen in a callback to ExecuteRequest
//so that there is no problem wrapping the HttpResponseMessage up in a using for both
//sync/async
return asyncClient.GetAsync(request.Uri, token);

var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = request.Uri
};

foreach (var header in request.Headers)
httpRequestMessage.Headers.Add(header.Key, header.Value);

return asyncClient.SendAsync(httpRequestMessage, token);
}
}
}

0 comments on commit c74bbf6

Please sign in to comment.