3

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?

Jonas Mohammed
  • 377
  • 4
  • 14
  • I think to start try comparing the request header that is being sent to server for both POSTMAN and from browser network tab. – Alvin Apr 23 '18 at 17:55
  • Is this api under your control or is this an external api that is returning the internal server error. – Igor Apr 23 '18 at 17:56
  • @Igor It's an external api, I have no control over it. I'm not sure where I can see all the headers, debug says they are all null/empty except bearer. https://pastebin.com/vFjAWvCP – Jonas Mohammed Apr 23 '18 at 18:04
  • Screenshot of debug: https://i.imgur.com/EcaNTxH.png – Jonas Mohammed Apr 23 '18 at 18:10
  • @user1672994 Yeah I tried that earlier, but I have no experience with Fiddler. So far it tracks the postman request and the search request that works, however I can't get it to show the request that fails. https://i.imgur.com/o6B0NyC.png #13 is postman, #21 is "search" that works, but nothing shows up from the last request. – Jonas Mohammed Apr 23 '18 at 18:33

3 Answers3

0

Two things you need to do:

  1. you need to check request header via fiddler, if finding any gap fill it up.
  2. most important thing, log complete URL with parameters in code. Use same URL in postman, hopefully you will get the issue.
Pang
  • 9,564
  • 146
  • 81
  • 122
Suhail
  • 71
  • 4
  • Did both these things; 1: I can't track the 500 in Fiddler, not sure if I'm doing something wrong.https://i.imgur.com/o6B0NyC.png 2: I already copy pasted it from output into postman, it works. – Jonas Mohammed Apr 23 '18 at 18:44
  • setup a call with vendor. And check with them .what happened to your request.they might come up with some logs. – Suhail Apr 23 '18 at 19:05
0

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.

Jonas Mohammed
  • 377
  • 4
  • 14
-1

One solution is to DeserializeObject what comes our of Postman. I had a similar issue when my automated tests worked but not the Postamn requests.

In my case I did s.th like this

try
{
   Foo[prop] = JsonConvert.DeserializeObject(stringValue);
}
catch
{
   Foo[prop] = stringValue;
}
Mali Tbt
  • 679
  • 1
  • 8
  • 12