Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bad request(400) error!
#1
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
Reply
#2
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
Reply
#3
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)
Reply
#4
(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
Reply
#5
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
Reply
#6
(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?
Reply
#7
This can be fixed only by the people running the server so unfortunately there is nothing you can do at this point.
Reply
#8
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]
Reply
#9
(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'))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Empty response to request causing .json() to error t4keheart 1 716 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 1,508 Jun-18-2020, 08:07 AM
Last Post: buran
  urllib request error 404 Coco 2 2,828 May-11-2019, 02:47 PM
Last Post: Larz60+
  Error in request, cache key a21250450 2 1,573 Apr-02-2019, 11:20 AM
Last Post: a21250450

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020