Python Forum
how can I correct the Bad Request error on my curl request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I correct the Bad Request error on my curl request
#1
I have this request example :

curl –X POST https://api.apifonica.com/v2/accounts/{a...}/messages \
-H 'Content-Type: application/json' \
-d '{
"from": "35315313424",
"to": "447860041755",
"text": "Apifonica API provides a really cool SMS messaging service"
}' \
-u {accountSID}:{authToken}

I have already coded it this way using the python request module

import requests
from requests.auth import HTTPBasicAuth


h = {"Content-Type" : "application/json"}

d = {
    "from": "35315313424",
    "to": "447860041755",
    "text": "Apifonica API provides a really cool SMS messaging service"
}
u = HTTPBasicAuth('my accountSID' , 'my authToken')

   

url = 'https://api.apifonica.com/v2/accounts/{my accountSID here}/calls'

result = requests.post(url, auth =u ,data= d, headers = h)
print(result.text)
but I'm having this error :
Error:
{"status_code":400,"error_code":10001,"error_text":"Bad Request","uri":"https://www.apifonica.com/en/docs/api/rest/errors#10001"}
The error code reference from the API provider said that ''The request could not be understood by the Apifonica server due to malformed syntax.
Please check the brackets and quotes in your request.''
I don't know where I'm wrong with my syntax, I have already research on other questions similar to this my own here on this forum and on google but couldn't figure out the solution
Reply
#2
Try this.
import requests

headers = {
    "Content-Type": "application/json",
}

data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
        "Apifonica API provides a really cool SMS messaging service"\n}'

response = requests.post(
    "https://api.apifonica.com/v2/accounts/%7BaccountSID%7D/calls",
    headers=headers,
    data=data,
    auth=("{accountSID}", "{authToken}"),
)
Or with HTTPBasicAuth.
import requests
from requests.auth import HTTPBasicAuth

headers = {
    "Content-Type": "application/json",
}

data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
        "Apifonica API provides a really cool SMS messaging service"\n}'

u = HTTPBasicAuth('my accountSID' , 'my authToken')
response = requests.post(
    "https://api.apifonica.com/v2/accounts/%7BaccountSID%7D/calls",
    headers=headers,
    data=data,
    auth=u,
)
Reply
#3
(Oct-01-2021, 09:05 AM)snippsat Wrote: Try this.
import requests

headers = {
    "Content-Type": "application/json",
}

data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
        "Apifonica API provides a really cool SMS messaging service"\n}'

response = requests.post(
    "https://api.apifonica.com/v2/accounts/%7BaccountSID%7D/calls",
    headers=headers,
    data=data,
    auth=("{accountSID}", "{authToken}"),
)
Or with HTTPBasicAuth.
import requests
from requests.auth import HTTPBasicAuth

headers = {
    "Content-Type": "application/json",
}

data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
        "Apifonica API provides a really cool SMS messaging service"\n}'

u = HTTPBasicAuth('my accountSID' , 'my authToken')
response = requests.post(
    "https://api.apifonica.com/v2/accounts/%7BaccountSID%7D/calls",
    headers=headers,
    data=data,
    auth=u,
)

I have tried it but i have this error on both the HTTPBasicAuth and the normal request
Error:
{"status_code":400,"error_code":10012,"error_text":"Validation error. Please enter valid parameter values.","parameters":["accountSID"],"errors":{"accountSID":["SID length is invalid."]},"uri":"https://www.apifonica.com/en/docs/api/rest/errors#10012"}
Reply
#4
Try with i guess that {} shall not be there so no need to url encode.
"https://api.apifonica.com/v2/accounts/accountSID/calls"
Reply
#5
(Oct-01-2021, 02:05 PM)snippsat Wrote: Try with i guess that {} shall not be there so no need to url encode.
"https://api.apifonica.com/v2/accounts/accountSID/calls"

Still the same previous error from my first code
Reply
#6
import requests

headers = {
    "Content-Type": "application/json",
}
data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
     "Apifonica API provides a really cool SMS messaging service"\n}'

response = requests.post(
    "https://api.apifonica.com/v2/accounts/aa9999/messages",
    headers=headers,
    data=data,
    auth=("aa9999", "111111"),
)

print(response.content)
Output:
b'{"status_code":401,"error_code":10010,"error_text":"Unauthorized","uri":"https://www.apifonica.com/en/docs/api/rest/errors#10010"}'
If run this code do you get error 10012?
I do get 10010 Unauthorized as except as i have not have signed up and gotten accountSID and token.
Reply
#7
(Oct-02-2021, 09:10 AM)snippsat Wrote:
import requests

headers = {
    "Content-Type": "application/json",
}
data = '{\n"from": "35315313424",\n"to": "447860041755",\n"text":\
     "Apifonica API provides a really cool SMS messaging service"\n}'

response = requests.post(
    "https://api.apifonica.com/v2/accounts/aa9999/messages",
    headers=headers,
    data=data,
    auth=("aa9999", "111111"),
)

print(response.content)
Output:
b'{"status_code":401,"error_code":10010,"error_text":"Unauthorized","uri":"https://www.apifonica.com/en/docs/api/rest/errors#10010"}'
If run this code do you get error 10012?
I do get 10010 Unauthorized as except as i have not have signed up and gotten accountSID and token.


here is the Account SID
accxxxxxxxxxxxxxx

Account token
aut306xxxxxxxxxxxxxxx
all for test
snippsat write Oct-02-2021, 06:35 PM:
x out log in info
Reply
#8
I have xxx out you authentication info as you should not post that.
I can not get authentication to work,not in Request or curl get 10010 Unauthorized in both.
Have you test that it work in curl?
Can run curl online here if on Windows and have not installed curl.
Reply
#9
(Oct-02-2021, 06:44 PM)snippsat Wrote: I have xxx out you authentication info as you should not post that.
I can not get authentication to work,not in Request or curl get 10010 Unauthorized in both.
Have you test that it work in curl?
Can run curl online here if on Windows and have not installed curl.

the tokens id the one I created for test only is not my real api tokens
let me quickly check for the curl
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 374 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Is it possible to add a delay right after a request.get() cubangt 6 2,774 Sep-07-2023, 09:29 AM
Last Post: shoesinquiry
  How would I alter this to a single api request? SuchUmami 1 750 Jun-24-2023, 08:30 AM
Last Post: ferdnyc
  Request Dependency warning thetechnodino 0 938 Dec-20-2022, 02:12 AM
Last Post: thetechnodino
  How to output one value per request of the CSV and print it in another func? Student44 3 1,347 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Unable to request image from FORM Data usman 0 996 Aug-18-2022, 06:23 PM
Last Post: usman
  Repeat request by else stsxbel 2 1,179 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,229 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  request.get to read large response pythonlearner1 7 3,207 Apr-05-2022, 08:21 PM
Last Post: pythonlearner1
  alternative to using request module in python vlearner 2 1,341 Feb-05-2022, 09:35 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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