1

I have been testing the musicbrainz API and I found a problem for me. When the title has an '&' in the name the query returns the wrong results. For example: The title is 'auf & ab' and the query returns a title named 'auf, auf, auf'.

I sort of fixed this by replacing '&' with 'and' like this:

if (title.includes('&')){title = title.replace('&','and')}

This returns the correct results. I am not sure if this is the way to solve this issue. This is my query: https://musicbrainz.org/ws/2/recording/?query=recording:auf%20&%20ab%20%26%26%20artist:montez&fmt=json&limit=5

Flitschi
  • 73
  • 5
  • 1
    when I decoded your url.. you have *" & "* and not *"&"*.. using and also seems to give the same results in your url `https://musicbrainz.org/ws/2/recording/?query=recording:auf&ab&&artist:montez&fmt=json&limit=5` in short.. the spacebar is the problem(eg: turn `auf & ab` to `auf&ab`) – The Bomb Squad Dec 14 '21 at 12:48
  • `auf&ab` doesn't work either. Only thing that worked so far is replacing `&` with `and` or `%26`. Maybe that is the best solution anyways... – Flitschi Dec 14 '21 at 13:12
  • what do you mean by it doesn't work? check [this repl](https://b17dee8c-7f21-4efe-bb02-afa2e8746782.id.repl.co/) and see how much it does work – The Bomb Squad Dec 14 '21 at 14:25
  • Yes it does work as in it gives me results but it returns 'auf, auf, auf' as title, not 'auf & ab'. encodeURIComponent() seems to be a good solution for me. Thanks for your help! – Flitschi Dec 14 '21 at 14:40

1 Answers1

0

I think you need to encode the "space" (with %20) and the "&" (with %26) chars, since they are used in url the & will be decoded as another new parameter in query string; try with this:

https://musicbrainz.org/ws/2/recording/?query=recording:auf%20%26%20ab&artist:montez&fmt=json&limit=5

In this way, your search "auf & ab" -> "auf%20%26%20ab"; you can achieve this result using the encodeURIComponent("auf & ab")

Edit: changed encodeURI -> encodeURIComponent

MatteoPHRE
  • 358
  • 4
  • 10
  • I mean once you run the request.. the destination already gets encoded as you see the url they come out with in the end ;-; – The Bomb Squad Dec 14 '21 at 12:51
  • Thanks for your answer, unfortunately it doesn't work. encodeURI doesn't encode the & as %26 but leaves it as & – Flitschi Dec 14 '21 at 13:05
  • 1
    You're welcome. Hovewer, my bad, the method is "encodeURIComponent" (memory plays tricks sometimes :) ) – MatteoPHRE Dec 14 '21 at 13:39