Search code examples
node.jsmusicbrainz

Search query with & in title gives wrong results


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


Solution

  • 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