i send the request to Nyckel Service and get the response back as text but the response i get is invalid format i am working on implement image classification :
the result i need to get is this depends on the image i send it through the POST request for Nyckel Service :
{
"labelName": "harissa",
"labelId": "label_684pbumtvbzp3k9q",
"confidence": 0.76
}
@api_view(['POST','GET'])
def addProductByNyckel(request):
data = request.data
image = data['image']
url = 'https://www.nyckel.com/v1/functions/7aaigszss2ejx7t8/invoke/?format=json'
header = {
'Content-type': 'application/json', 'Accept': 'text/plain'
}
urlimg = 'http://127.0.0.1:8000/media/images/chamia_2lJVXBC.jpg'
img = requests.get(urlimg,params=request.GET)
m = img.content
result = requests.post(url,m, headers=header)
dict = result.json()
labelName= dict.get("labelName"),
labelId = dict.get("labelId"),
confidence = dict.get("confidence")
return Response(dict )
`
the error message is :
**{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-983824a0cb2b204855f4387cf92f2dca-780a95630f9dc3d6-00",
"errors": {
"$": [
"'0x89' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
],
"input": [
"The input field is required."
]
}
}**
CodePudding user response:
As per nickle documentation you need to sent it via multipart request.
here is sample working request
import requests
url = 'https://www.nyckel.com/v1/functions/7aaigszss2ejx7t8/invoke/?format=json'
file = open('/tmp/sharukh.jpeg', 'rb') # it has to be file content
result = requests.post(url, files={'data': file}) # posting multipart payload
dict = result.json()
print(dict, result.status_code)