Python Forum
how can I correct the Bad Request error on my curl request - 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: how can I correct the Bad Request error on my curl request (/thread-35125.html)



how can I correct the Bad Request error on my curl request - tomtom - Oct-01-2021

I have this request example :

curl –X POST https://api.apifonica.com/v2/accounts/{accountSID}/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


RE: how can I correct the Bad Request error on my curl request - snippsat - Oct-01-2021

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,
)



RE: how can I correct the Bad Request error on my curl request - tomtom - Oct-01-2021

(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"}



RE: how can I correct the Bad Request error on my curl request - snippsat - Oct-01-2021

Try with i guess that {} shall not be there so no need to url encode.
"https://api.apifonica.com/v2/accounts/accountSID/calls"



RE: how can I correct the Bad Request error on my curl request - tomtom - Oct-02-2021

(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


RE: how can I correct the Bad Request error on my curl request - snippsat - Oct-02-2021

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.


RE: how can I correct the Bad Request error on my curl request - tomtom - Oct-02-2021

(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


RE: how can I correct the Bad Request error on my curl request - snippsat - Oct-02-2021

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.


RE: how can I correct the Bad Request error on my curl request - tomtom - Oct-03-2021

(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