4

I am building a program that uses Bing's search API and common lisp with the Drakma library to display some results but for some reason have an error when sending a longer length query It doesn't display any of the results at all. It works for shorter length queries. I am using a temp account for this question. I have the following code.

(defun get-rid-of-spaces (var)
 (cl-ppcre:regex-replace-all " " var "%20"))


(defun print-bing (search-term)
  (format nil "https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27~a%27&Options=%27DisableLocationDetection%27&$format=json&$top=1" (get-rid-of-spaces search-term)))

(defun drakma-bing (search-term)
 (drakma:http-request (print-bing search-term)
           :basic-authorization
           '("bob.hammerston@mailinator.com" "L2gbaj+s1/KW/+ifAa9HrP0C1/kClpF4InH48Lw8UNc")))

(defun convert-to-string (response)
 (map 'string #'code-char response))

And then I call this but it only works for short search terms and I can't figure out why. This doesn't work:

(convert-to-string (drakma-bing "what is the largest man in the world"))

But this does

(convert-to-string (drakma-bing "what is"))

Any idea why?

Thanks.

Edit:

I tried encoding the print-bing function by hand instead of using that function with a longer string and it still doesn't work so there must be an error with Drakma. I tried typing the domain into the web browser by hand and it works so that is why I think the error is with Drakma.

phlie
  • 1,335
  • 3
  • 10
  • 19
  • How long are your queries? At least some other questions here mention a maximum length. E.g., http://stackoverflow.com/questions/15334531/what-are-the-query-length-limits-for-the-bing-websearch-api. – Joshua Taylor Jan 23 '16 at 22:49
  • 1
    And what's actually in your search term? Your get rid of spaces function is very brittle. You should be url-encoding it, because spaces aren't the only thing that need special treatment. – Joshua Taylor Jan 23 '16 at 22:51
  • I used the examples given but tried many different ones making sure not to use punctuation. Any one of three or four words doesn't seem to work. I will look into url encoding but even manually entering it with %20 as spaces doesn't seem to work. – phlie Jan 23 '16 at 22:58

1 Answers1

5

You need to use + instead of %20 for spaces.

(defun get-rid-of-spaces (var)
 (cl-ppcre:regex-replace-all " " var "+"))
jkiiski
  • 8,206
  • 2
  • 28
  • 44
  • Well I feel like an idiot. The documentation for bing said to use %20 but that is so simple. Thanks. – phlie Jan 24 '16 at 10:38