Python Forum
Bad request(400) error! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Bad request(400) error! (/thread-15327.html)



Bad request(400) error! - manhsv - Jan-13-2019

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


RE: Bad request(400) error! - Larz60+ - Jan-13-2019

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


RE: Bad request(400) error! - mlieqo - Jan-13-2019

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)



RE: Bad request(400) error! - manhsv - Jan-14-2019

(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


RE: Bad request(400) error! - mlieqo - Jan-14-2019

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


RE: Bad request(400) error! - manhsv - Jan-14-2019

(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?


RE: Bad request(400) error! - mlieqo - Jan-14-2019

This can be fixed only by the people running the server so unfortunately there is nothing you can do at this point.


RE: Bad request(400) error! - manhsv - Jan-15-2019

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]


RE: Bad request(400) error! - hbknjr - Jan-15-2019

(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'))