0

This is the code I have so far:

        [HttpGet]
        [Route("api/search-music")]
        public async Task<List<SongFilterPreview>> GetSongs([FromUri] string searchTerm)
        {
            var token = await ServiceContext.TokenService.isAccessTokenExpired();
            HttpClient client = new HttpClient();
            UriBuilder builder = new UriBuilder(searchURL);
            builder.Query = "type=searchByValue&&searchTerm=" + searchTerm;
          
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            HttpResponseMessage message = client.GetAsync(builder.Uri).Result;

            string result = message.Content.ReadAsStringAsync().Result;
            dynamic items = new JavaScriptSerializer().Deserialize<List<object>>(result);

            List<SongFilterPreview> songs = new List<SongFilterPreview>();
            foreach(var item in items)
            {
                var sfp = new SongFilterPreview()
                {
                    songId = item["id"],
                    songName = item["title"],
                    Performer = item["group"],
                    songUrl = await CreateMediaUrl(item["id"])
                    
                };
                songs.Add(sfp);
            }
            return songs;
        }


 public async Task<string> CreateMediaUrl(int songId)
        {
           
            var playToken = await ServiceContext.TokenService.isPlayTokenExpired();
            var objectData = new
              {
                  playToken = playToken,
                  secure = true
              };

             HttpClient client = new HttpClient();
             client.BaseAddress = new Uri("https://apis.tunify.com/t4-media-service/zone/529155/track/" + songId + "/media-url/");
             client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjUyOTE1NSwiem9uZUlkIjo1MjkxNTUsImFwcElkIjo2NSwiY2xpZW50X2lkIjoiNjUiLCJhdXRob3JpdGllcyI6WyJSRUFEX1BSSVZJTEVHRSJdLCJzY29wZSI6WyJyZWFkIl0sImlhdCI6MTYxNDc3MDUwMSwiZXhwIjoxNjE0ODQyNTAxfQ.xmooyG3oFccoDNsTEsWlloLhAm0DgIw0_GFJ1uLXkZk");
             var content = new StringContent(new JavaScriptSerializer().Serialize(objectData), Encoding.UTF8, "application/json");
             var message = await client.PostAsync("https://apis.tunify.com/t4-media-service/zone/529155/track/" + songId + "/media-url/", content);

           
            string result = message.Content.ReadAsStringAsync().Result;
            dynamic item = new JavaScriptSerializer().Deserialize<object>(result);

             var oneUrl = "";
             foreach (var url in item)
             {
                 oneUrl = url["url"];
             }

            return oneUrl;
        }

I want iterate through the list of songs and fetch the url for each song, but my application doesn't send me response. I know what the problem is, it is CreateMediaUrl (client.SendPost) but I don't know who to fix it. when i send request from postman, i just can see "Sending request". How can i fix this?

jps
  • 20,041
  • 15
  • 75
  • 79
user237
  • 41
  • 3
  • 5
    Don't use `.Result`, you should be `await`ing the tasks. – DavidG Mar 03 '21 at 14:48
  • I tried but it doesn't work – user237 Mar 03 '21 at 14:51
  • Don't use `JavaScriptSerializer` either. That's an obsolete class used used as a stop-gap 10 years ago. It doesn't support the JSON conventions introduced since, eg the ISO8601 format for dates. Even ASP.NET Web API used Json.NET before it was replaced by System.Text.Json. – Panagiotis Kanavos Mar 03 '21 at 14:51
  • It's really not clear what your error is anyway. "doesn't send me response" - what does that mean? Have you tried stepping through your code? Is the `GetSongs` method useful here, if not then remove it from this post. We need a [mre] – DavidG Mar 03 '21 at 14:52
  • @user237 the code you posted has several obvious problems. They may not be directly related to your current problem, but they **have** to be fixed. Using `.Result` in an async method is pointless - that's why you added `async` in the first place, to be able to use `await.` Using `JavaScriptSerializer` will fail with JSON strings that use proper dates. – Panagiotis Kanavos Mar 03 '21 at 14:53
  • @user237 besides, HttpClient doesn't send responses, it sends *requests* and receives responses. – Panagiotis Kanavos Mar 03 '21 at 14:54
  • @DavidG What should I use instead of JavaScriptSerializer? I want convert json object to custom object – user237 Mar 03 '21 at 15:08
  • @DavidG HttpClient doesn't create url for each song – user237 Mar 03 '21 at 15:24
  • Maybe remove the base address when you are already sending the absolute uri in post. – Anup Sharma Mar 03 '21 at 16:49

0 Answers0