'Malformed or invalid request' with clarifai api - clarifai

what is wrong with the following request for clarifai api-
import requests
image_url='https://samples.clarifai.com/food.jpg'
api='Key cb03ceba3c8842aeadd55dcb2f0be152'
headers = {
'Authorization': api,
'Content-Type': 'application/json',
}
data = '{"inputs": [{"data": {"image": {"url": image_url}}}]}'
url='https://api.clarifai.com/v2/models/bd367be194cf45149e75f01d59f77ba7/outputs'
response = requests.post(url=url, headers=headers, data=data)
print(response.status_code, response.json())
i keep hitting this error-
400 {'status': {'code': 11102, 'description': 'Invalid request', 'details': 'Malformed or invalid request'}}

Looks like you need to use:
'{"inputs": [{"data": {"image": {"url": "' + image_url + '"}}}]}'
Since the single quotes are creating a string, you can't just add in a variable directly, but need to concatenate it. You were literally sending the text image_url and not the actual value of the variable image_url in that statement.

You need to correct the header as well to get a valid response
header = {'Authorization': 'Key '+ api_key ,"Content-Type": "application/json"}

you have to convert the data into JSON and json.dumps() will convert the data into JSON.
data = {"inputs": [{"data": {"image": {"url": image_url}}}]}
json_data = json.dumps(data)

I had the same problem; I solved it by converting the variable holding the image URL to a string. In your case, just make the following change:
data = '{"inputs": [{"data": {"image": {"url": image_url.tostring}}}]}'

Related

missing keyword parameter is clearly included in api call

I'm trying to set up a basic API call to Amadeus. All the correct parameters are included and I'm grabbing a fresh token for each call.
It's a React/Flask application.
Here's my endpoint in app/routes.py:
#app.route('/search', methods=['GET'])
#cross_origin(origin='*')
def get_flights():
api_key = os.environ.get('amadeus_api_key', None)
api_secret = os.environ.get('amadeus_api_secret', None)
# first, you must get an access token using your Amadeus credentials
token_request = requests.post(
'https://test.api.amadeus.com/v1/security/oauth2/token',
data = {
'grant_type': 'client_credentials',
'client_id': api_key,
'client_secret': api_secret
}
)
token = token_request.json()['access_token']
bearer = 'Bearer {}'.format(token)
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',
headers = {
'Authorization': bearer
},
data = {
'subType': 'AIRPORT',
'keyword': 'BOS'
}
)
print(locations.json())
# example:
# https://test.api.amadeus.com/v1/reference-data/locations
# ?subType=AIRPORT&keyword=BOS
return jsonify({'token': token})
Here's the error:
{'errors': [{'status': 400, 'code': 32171, 'title': 'MANDATORY DATA MISSING', 'detail': 'Missing mandatory query parameter', 'source': {'parameter': 'keyword'}}]}
As you can see in the /search endpoint, the keyword parameter is clearly included.
What gives? Am I missing something?
The request to get the locations is not correctly built as it is sending subType and keyword as body instead of query parameters. According to requests documentation, you need to use params:
locations = requests.get(
'https://test.api.amadeus.com/v1/reference-data/locations',
headers = {
'Authorization': bearer
},
params = {
'subType': 'AIRPORT',
'keyword': 'BOS'
}
)

Odoo controllers avoid json-rpc when type="json"

I've the following route:
#http.route([
'/whatever/create'
], auth="none", type='json', methods=['POST'], csrf=False)
which I'm using to send a post request with json data on its body.
Is there any way to avoid using json-rpc responses on routes of type="json"? I would like to just answer plain json.
If it is not possible, is there any way to get the json data placed on the body request by using `type="http"?
#http.route('/whatever/create', auth='public', methods=['POST'], type='http')
def index(self, **kw):
data = request.httprequest.data
return 'success'
Above code defined in Odoo
url = "http://localhost:8069/whatever/create"
param = {
"type_operation": "PTI",
"label": "",
}
headers = {'Content-type': 'text/plain'}
r = requests.post(url, data=json.dumps(param), headers=headers)
Above code i have requested from a py file
While sending request you should change Content-type
'Content-type': 'application/json' --- > 'Content-type': 'text/plain'
Also while returning it only accept String
return {'status': 'success'} ---> return 'success'

ADF Create Pipeline Run - Parameters

I need to trigger a ADF Pipeline via REST API and pass a parameter in order to execute the pipeline for the given ID (parameter).
With sparse documentation around this, I am unable to figure out how to pass parameters to the URL
Sample:
https://management.azure.com/subscriptions/asdc57878-77fg-fb1e8-7b06-7b0698bfb1e8/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/df-datafactory-dev/pipelines/pl_StartProcessing/createRun?api-version=2018-06-01
I tried to send parmaters in the request body but I get the following message depending on how params are sent
{
"message": "The request entity's media type 'text/plain' is not supported for this resource."
}
I tried using python requests :
import requests
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01"
payload = " \"parameters\": {\r\n “stateID”: “78787878”\r\n}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer adsasdasdsad'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
I tried to put the parameter in the payload (body)
Paramters can be passed within body
python sample:
import requests
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01"
payload = "{\"stateID\":1200}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer adsasdasdsad'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
You have to use a parameter name as post
url = "https://management.azure.com/subscriptions/adsad-asdasd-adasd-adasda-adada/resourceGroups/dev-rg/providers/Microsoft.DataFactory/factories/datafactory-dev/pipelines/pl_Processing/createRun?api-version=2018-06-01 -d '{"stateID"="78787878"}'
microsoft docs for your reference :
https://learn.microsoft.com/en-us/rest/api/datafactory/pipelines/create-run
You have to pass them as the POST body.
To pass more than one parameter the body this looks like:
{
"param1": "param1value"
,"param2":"param2value"
}

cannot subscribe to onedrive API

I tried to subscribe to onedrive webhooks by hitting
https://graph.microsoft.com/v1.0/subscriptions
https://graph.microsoft.com/beta/subscriptions
parameters are:
"changeType": "created,updated,deleted",
"notificationUrl": url.
"resource": "me/drive/root",
"clientState": "client-specific string",
"expirationDateTime": "2018-01-01T11:23:00.000Z",
I am getting an error like below:
{ error:
{ code: 'InvalidRequest',
message: 'Server could not process subscription creation payload.',
innerError:
{ 'request-id': 'id',
date: '2018-10-16T09:16:46' } } }
I am trying it in my local.
Is there any solution ?
Make sure you post a json request, that means:
Request body is a json string;
'Content-Type' field in headers, and value is 'application/json'
If you use Python, there is a shortcut:
import requests
url = "https://graph.microsoft.com/beta/subscriptions"
headers = {'Authorization': 'Bearer ' + "YOUR_TOKEN"}
data = {
"changeType": "created,updated,deleted",
"notificationUrl": url.
"resource": "me/drive/root",
"clientState": "client-specific string",
"expirationDateTime": "2018-01-01T11:23:00.000Z"
}
resp = requests.post(headers=headers, json=data)
I had the same problem trying the request with Postman. The body was in x-www-form-urlencoded format. It worked when I changed the body format to raw and specified JSON.
Not working "x-www-form-urlencoded" format
Working "raw" format
It looks like MS Graph API only accepts certain input formats. Hope this will help!

Posting JSON object through WinJS.XHR

I'm having abit of problems posting data from WinJS.xhr to a PHP script.
"obj" is a stringified JSON object
WinJS.xhr({
type: "POST",
url: dataUrl,
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: obj,
})
However the $_POST variable is always empty.
I've tried changing content-types, and escaping the object but no luck :(
Your content-type when posting json should typically be application/json
Secondly make sure you 'stringify' your json object.
Based on: Post JSON data to web services in Windows 8
WinJS.xhr({
type: "post",
url: dataUrl,
headers: { "Content-type": "application/json" },
data: JSON.stringify(obj)
})
Figured out a soloution.
Incase anyone has the same problem i got it working by removing headers from the xhr, and getting the post data # server side with this code:
$data = file_get_contents('php://input');
$data = (array) json_decode($data);