0

Here is the code:

import requests

url = "https://api.yelp.com/v3/businesses/search"

response = requests.get("https://api.yelp.com/v3/businesses/search")
print(response)

and I got this error:

PS D:\HelloWord> cd "d:\HelloWord"
PS D:\HelloWord> python -u "d:\HelloWord\app.py"
<Response [400]>

And I don't know what is wrong here I follow up the tutorial and is the same. Thank you in advance!

iollo
  • 47
  • 8
  • You probably need an API key or something else to interact with the API. The error message from curl is `Authorization is a required parameter.` – Phix Apr 28 '21 at 16:05

4 Answers4

1

To understand what is happening, you need to look at the response object's properties. Take a look at the docs for response object. Printing response.text will probably be informative.

Cargo23
  • 3,064
  • 16
  • 25
1

HTTP status 400 means there is some problem from the client-side. Please go through the doc again. Looking at the URL it seems that this Yelp API will need some sort of API key as param or some request header. These kinds of API are often not free for use.

For details place a line.

print(response.text)

It will give this response.

{"error": {"code": "VALIDATION_ERROR", "description": "Authorization is a required parameter.", "field": "Authorization", "instance": null}}

Thus it requires authorization details in some way which you can find on the doc itself.

vijaydeep
  • 395
  • 3
  • 12
  • Now I got this error: {"error": {"code": "VALIDATION_ERROR", "description": "'BearerSxLTnW2IRQCthSn_rFIeweyiEUhgvKMaD0Mr_C24c8Ain1b5e7s7PFtTeve0S51inmTerWGKMLlIYehKZR2YJiA-4LWhzL1I8yugrhL7UffCieymWTh-XesPeY6JYHYx' does not match '^(?i)Bearer [A-Za-z0-9\\\\-\\\\_]{128}$'", "field": "Authorization", "instance": "BearerSxLTnW2IRQCthSn_rFIeweyiEUhgvKMaD0Mr_C24c8Ain1b5e7s7PFtTeve0S51inmTerWGKMLlIYehKZR2YJiA-4LWhzL1I8yugrhL7UffCieymWTh-XesPeY6JYHYx"}} PS D:\HelloWord> – iollo Apr 28 '21 at 16:36
  • I add the this piece of code: ``` url = "https://api.yelp.com/v3/businesses/search" api_key = "SxLTnW2IRQCthSn_rFIeweyiEUhgvKMaD0Mr_C24c8Ain1b5e7s7PFtTeve0S51inmTerWGKMLlIYehKZR2YJiA-4LWhzL1I8yugrhL7UffCieymWTh-XesPeY6JYHYx" header = { "Authorization": "Bearer" + api_key } parametri = { "location": "NYC" } response = requests.get(url, headers=header, params=parametri) ``` – iollo Apr 28 '21 at 16:36
  • 1
    There is a standard format of Bearer . Thus space is needed after Bearer which is missing as displayed in the comment. Please fix that. – vijaydeep Apr 28 '21 at 16:46
  • Like this: headers={ 'Authorization': 'Bearer SxLTnW2IRQCthSn_rFIeweyiEUhgvKMaD0Mr_C24c8Ain1b5e7s7PFtTeve0S51inmTerWGKMLlIYehKZR2YJiA-4LWhzL1I8yugrhL7UffCieymWTh-XesPeY6JYHYx'}) – iollo Apr 28 '21 at 17:01
1

According to docs, you need to set api_key into header of your request:

requests.get("https://api.yelp.com/v3/businesses/search", headers={'Authorization': 'Bearer <Your Api Key>'})

Read more about 400 error

Nikita Zhuikov
  • 1,012
  • 8
  • 11
0

Requests will automatically decode content from the server. Most unicode charsets are seamlessly decoded.

>>> import requests
>>> r = requests.get('https://api.yelp.com/v3/businesses/search')
>>> r.text

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property: