2

I'm trying to access an API where one of the parameter has a \ in it's name (Axwell /\ Ingrosso).

If I access the API using the website API panel directly, I get the results using Axwell /\\ Ingrosso.

The request URL then becomes https://oauth-api.beatport.com/catalog/3/tracks?facets=artistName%3AAxwell+%2F%5C%5C+Ingrosso&perPage=150

If I try to access the same API endpoint using Python request, I get an empty response.

This is what I'm trying

r = session.get('https://oauth-api.beatport.com/catalog/3/tracks', 
               params={'facets': 'artistName:Axwell /\\ Ingrosso', 
                       'perPage': 150})

I've also tried using it without the backslash in Python request but even that outputs an empty response. What am I missing here?

Yin Yang
  • 1,776
  • 3
  • 26
  • 48

1 Answers1

4

You need to double the backslashes:

'artistName:Axwell /\\\\ Ingrosso'

or use a raw string literal by prefixing the string literal with an r:

r'artistName:Axwell /\\ Ingrosso'

In Python string literals, backslashes start escape sequences, and \\ signifies an escaped escape sequence, e.g. a regular backslash character without specific meaning:

>>> print 'artistName:Axwell /\\ Ingrosso'
artistName:Axwell /\ Ingrosso
>>> print 'artistName:Axwell /\\\\ Ingrosso'
artistName:Axwell /\\ Ingrosso
>>> print r'artistName:Axwell /\\ Ingrosso'
artistName:Axwell /\\ Ingrosso

or as encoded URLs produced by requests:

>>> import requests
>>> requests.Request('GET',
...     'https://oauth-api.beatport.com/catalog/3/tracks',
...     params={'facets': 'artistName:Axwell /\\ Ingrosso',
...             'perPage': 150}).prepare().url
'https://oauth-api.beatport.com/catalog/3/tracks?facets=artistName%3AAxwell+%2F%5C+Ingrosso&perPage=150'
>>> requests.Request('GET',
...     'https://oauth-api.beatport.com/catalog/3/tracks',
...     params={'facets': 'artistName:Axwell /\\\\ Ingrosso',
...             'perPage': 150}).prepare().url
'https://oauth-api.beatport.com/catalog/3/tracks?facets=artistName%3AAxwell+%2F%5C%5C+Ingrosso&perPage=150'
>>> requests.Request('GET',
...     'https://oauth-api.beatport.com/catalog/3/tracks',
...     params={'facets': r'artistName:Axwell /\\ Ingrosso',
...             'perPage': 150}).prepare().url
'https://oauth-api.beatport.com/catalog/3/tracks?facets=artistName%3AAxwell+%2F%5C%5C+Ingrosso&perPage=150'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for the detailed explanation. But for some weird reason the same code works for my local machine, but does not work on my server machine. Any clues? – Yin Yang Dec 05 '14 at 12:43
  • @YinYang: define 'does not work' here. What does the API response tell you is wrong? – Martijn Pieters Dec 05 '14 at 12:46
  • It still outputs an empty response on the server. While I do get a response for the same code on my local machine. – Yin Yang Dec 05 '14 at 12:49
  • @YinYang: if you are running the **exact same request** on both machines (same headers, same query parameters, etc.) then perhaps you'll need to contact Beatport about that. Do make sure you are 100% certain the same request is sent. You could use [request logging](http://stackoverflow.com/a/16337639) to record *some* details for each request (the URL path, really) so you can compare. – Martijn Pieters Dec 05 '14 at 12:52