I'm trying to send GET requests to to the following API endpoints:
https://api.mediahound.com/1.3/search/all?PARAM_STRING
https://api.mediahound.com/1.3/graph/lookup?params=PARAMS_JSON
The first one works just fine with the following code (in both postman & c#):
var baseUri = new Uri("https://api.mediahound.com/1.3/search/all/");
using (var client = new HttpClient())
{
var res = "";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _oAuth2.Token);
Task task = Task.Run(async () => { res = await client.GetStringAsync(baseUri + searchInput + "?type=movie"); });
task.Wait();
.....
}
The second one I can't get to work and it gives me an InternalServerError
response. It currently looks like this:
public async Task<ObservableCollection<Movie>> GetMoviesAsync(string id)
{
var baseUri = new Uri("https://api.mediahound.com/1.3/graph/lookup?params=");
using (var client = new HttpClient())
{
var param = "{\n\"ids\":\n[\"" + id + "\"],\n\"components\":\n[\"primaryImage\",\n\"keyTraits\"]\n}";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _oAuth2.Token);
var res = await client.GetAsync(baseUri + param);
if(!res.IsSuccessStatusCode)
{
throw new Exception("HttpClient Error: " + res.StatusCode); //InternalServerError 500
}
var content = await res.Content.ReadAsStringAsync();
.....
}
}
Simply copy pasting the baseUri
+ param
into postman, gives me the desired result, however I can't replicate that in the program no matter what I do, and I'm not sure how I should proceed with debugging the problem.
Anyone got some good ideas?
So I figured out the issue. Seems postman automatically escapes quotes etc. in the url, but c# doesn't. I solved the issue by adding Uri.EscapeUriString()
on the param
.