Python Forum

Full Version: Bad request(400) error!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I try to add Vietnamese accent by using API on this :link((on API: Add Accents) but bad request(400).
import requests
url = "http://api.openfpt.vn/vnaccent/add"

headers = {
    
    'api_key': "5268c1bfd9da46c996d50649b23b59f5",
    'Cache-Control': "no-cache"
    }


response = requests.request("-X GET",url,headers=headers)

print(response.text)
I'm a beginner to this. Rolleyes
400 error is:
Quote:6.5.1. 400 Bad Request

The 400 (Bad Request) status code indicates that the server cannot or
will not process the request due to something that is perceived to be
a client error (e.g., malformed request syntax, invalid request
message framing, or deceptive request routing).

It probably doesn't like the format of your header or the URL is incorrect
Hey manhsv,

if you look at the doc page for this 'add' service you can see it also has some required query parameters, in this case 'text'. So you need to also send this 'text' parameter as part of your request.

Also your request type is currently '-X GET' and '-X' is actually just a parameter specifying request method for the curl command so you can skip that and just go with GET or just simply using method requests.get() .

payload = {'text': 'some text here'}
response = requests.get(url, headers=headers, data=payload)
(Jan-13-2019, 06:19 PM)mlieqo Wrote: [ -> ]Hey manhsv,

if you look at the doc page for this 'add' service you can see it also has some required query parameters, in this case 'text'. So you need to also send this 'text' parameter as part of your request.

Also your request type is currently '-X GET' and '-X' is actually just a parameter specifying request method for the curl command so you can skip that and just go with GET or just simply using method requests.get() .

payload = {'text': 'some text here'}
response = requests.get(url, headers=headers, data=payload)
Thanks for your reply. However,the code didn't work:
import requests
url = "http://api.openfpt.vn/vnaccent/add"

headers = {
    
    'api_key': "5268c1bfd9da46c996d50649b23b59f5",
    'Cache-Control': "no-cache"
    }
payload = {'text': 'do la mot nguoi dan ba dam dang'}

response = requests.get(url,headers=headers, data=payload)

print(response.text)
I change the code to:
print(response)
This case is 504. I search this is:
Quote:504 - Gateway Timeout
How to solve that problem? Blush
Your request seems ok, if you're getting 504 then the problem should be on the server side - check a short explanation here: https://stackoverflow.com/a/8494758
(Jan-14-2019, 09:17 AM)mlieqo Wrote: [ -> ]Your request seems ok, if you're getting 504 then the problem should be on the server side - check a short explanation here: https://stackoverflow.com/a/8494758
I got it but is there anyway to fix it?
This can be fixed only by the people running the server so unfortunately there is nothing you can do at this point.
I've checked, the service of this API doesn't work.But this case is POST function. I've tried another API. May you do an example for this? API's documentation is here(my api_key is on the previous post). Thank in advance

If you don't know Vietnamese, I've translated it into English
[Image: ncG9s9b.png]
(Jan-15-2019, 11:50 AM)manhsv Wrote: [ -> ]I've checked, the service of this API doesn't work.But this case is POST function. I've tried another API. May you do an example for this? API's documentation is here(my api_key is on the previous post). Thank in advance

Example:
import requests

name = "Nguyễn Văn Tuấn"
headers={
    "Cache-Control": "no-cache",
    "api_key": "5268c1bfd9da46c996d50649b23b59f5"
    }

r = requests.post('http://api.openfpt.vn/name2gender/json', headers=header, data = name.encode('utf-8'))