Flutter Post Request to a Flask API - python

i want to send a request Post from my App coded in Flutter in which there is an image converted in Base 64. Here is the following code that I am using :
Future<List<Result>> postJSON(String imageP, String iP, String port) async {
final String jsonEndpoint = "http://$iP:$port/todo/api/v1.0/tasks/mdrv";
final response = await http.post('$jsonEndpoint', body:
{
"id": "3",
"title_image": "Test",
"b64Image": "$imageP",
"done": "false",
},);
if (response.statusCode == 200){
List results = jsonDecode(response.body);
return results
.map(
(result) => new Result.fromJson(result))
.toList();
} else {
throw Exception('Erreur dans le chargement, veuillez réessayer');
}
}
But, when i do the request, i have the following TypeError on my Flask API :
description = JSON["b64Image"]
TypeError: 'NoneType' object is not subscriptable
I am using the following Python code :
def send_client():
Lresult_algo=[]
JSON = request.get_json()
id = JSON['id']
'description':JSON['b64Image']
server=Server(description)
description1=server.B64_array(description)
description2=Image.fromarray(description1)
description2.save(r"C:\Users\vince\Desktop\test2.png")
queryPath=r"C:\Users\vince\Desktop\test2.png"
Lresult_algo=server.send(queryPath)
maskedBodies_b64 = []
for matrice in Lresult_algo:
matrice1=matrice.astype('uint8')
maskedBodies_b64.append(base64.b64encode(cv2.imencode('.jpg', matrice1)[1]))
maskedBodies_b64=[str(b64) for b64 in maskedBodies_b64]
data = {
'Image_1' : maskedBodies_b64[0],
'Image_2' : maskedBodies_b64[1],
'Image_3' : maskedBodies_b64[2],
'Image_4' : maskedBodies_b64[3],
'Image_5' : maskedBodies_b64[4]
}
resp=json.dumps(data)
return resp
Do you think is this a typing problem ? How could I fix it ?

I changed my code like this but there is still the same mistake :
Future<List<Result>> postJSON(String imageP, String iP, String port) async {
final String jsonEndpoint = 'http://$iP:$port/api/v1.0/tasks/mdrv';
Map<String, dynamic> data = {
'id': 1,
'title_image': "Test",
'b64Image': "$imageP",
'done': false,
};
var client = new http.Client();
var body = jsonEncode(data);
var response = await client.post('$jsonEndpoint',headers: {"Content-Type": "application/json"}, body: body,);
if (response.statusCode == 200){
List results = jsonDecode(response.body);
return results
.map(
(result) => new Result.fromJson(result))
.toList();
} else {
throw Exception('Erreur dans le chargement, veuillez réessayer');
}
}

Related

How to pass text after video processing using WebRTC?

My task is to get the video stream from the client's webcam, analyze it and return the result as a string. Reading the documentation on aiortc and looking at their examples on git, I could only do this option:
async def offer(request):
params = await request.json()
offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
pc = RTCPeerConnection()
pcs.add(pc)
await server(pc, offer)
return web.Response(
content_type="application/json",
text=json.dumps(
{"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
),
)
pcs = set()
async def server(pc, offer):
#pc.on("connectionstatechange")
async def on_connectionstatechange():
print("Connection state is %s" % pc.connectionState)
if pc.connectionState == "failed":
await pc.close()
pcs.discard(pc)
#pc.on("track")
def on_track(track):
print("======= received track: ", track)
if track.kind == "video":
global new_video_track
new_video_track = Sign(track)
pc.addTrack(new_video_track)
#pc.on("datachannel")
def on_datachannel(channel):
global new_video_track
new_video_track.channel = channel
print("mounted channel")
#channel.on("message")
async def on_message(message):
if isinstance(message, str):
data = message.encode("utf-8")
else:
data = message
print("receive data: ", data)
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
async def on_shutdown(app):
# close peer connections
coros = [pc.close() for pc in pcs]
await asyncio.gather(*coros)
pcs.clear()
class Sign(VideoStreamTrack):
kind = "video"
def __init__(self, track):
super().__init__()
self.track = track
self.channel = None
async def recv(self):
frame = await self.track.recv()
# getting the result I need
if self.channel and self.sentence:
self.channel.send(json.dumps({'word': self.sentence}))
return frame
But this result is not satisfactory, because I need to return the frame. How to make it so that only my word is returned.
Here's the client.js
var pc = null;
var localVideo = document.querySelector("video#localVideo");
var serverVideo = document.querySelector("video#serverVideo");
navigator.mediaDevices.getUserMedia({
video: {
height: 1080,
width: 1920,
frameRate: {
max: 10
}
}
}).then(stream => {
localVideo.srcObject = stream;
localVideo.addEventListener('loadedmetadata', () => {
localVideo.play();
});
});
function negotiate () {
return pc.createOffer().then(function (offer) {
return pc.setLocalDescription(offer);
}).then(function () {
// wait for ICE gathering to complete
return new Promise(function (resolve) {
if (pc.iceGatheringState === 'complete') {
resolve();
} else {
function checkState () {
if (pc.iceGatheringState === 'complete') {
pc.removeEventListener('icegatheringstatechange', checkState);
resolve();
}
}
pc.addEventListener('icegatheringstatechange', checkState);
}
});
}).then(function () {
var offer = pc.localDescription;
return fetch('/offer', {
body: JSON.stringify({
sdp: offer.sdp,
type: offer.type,
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
});
}).then(function (response) {
return response.json();
}).then(function (answer) {
return pc.setRemoteDescription(answer);
}).catch(function (e) {
alert(e);
});
}
function start () {
var config = {
sdpSemantics: 'unified-plan',
iceServers: [{ urls: ['stun:stun.l.google.com:19302'] }]
};
pc = new RTCPeerConnection(config);
localVideo.srcObject.getVideoTracks().forEach(track => {
pc.addTrack(track);
});
pc.addEventListener('track', function (evt) {
console.log("receive server video");
if (evt.track.kind == 'video') {
serverVideo.srcObject = evt.streams[0];
}
});
ch = pc.createDataChannel("chat", {
ordered: false,
maxRetransmits: 0,
});
ch.addEventListener("message", function (evt) {
console.log(Date.now() - JSON.parse(evt.data).now);
});
document.getElementById('start').style.display = 'none';
negotiate();
document.getElementById('stop').style.display = 'inline-block';
}
function stop () {
document.getElementById('stop').style.display = 'none';
setTimeout(function () {
pc.close();
}, 500);
}
Thank you in advance and forgive the large chunks of code.
You can use jinja template. You can send only variable values.
This is an example of jinja template https://aiohttp-jinja2.readthedocs.io/en/stable/

Axios post request error: 422 Unprocessable Entity

I've been trying to build this stock prediction web app using Prophet model and FastAPI. I'm stuck at this problem where I'm getting console log error: 422 Unprocessable Entity.
Here's the react code to get API request:
async function GetAPI () {
const [predData, setPred] = React.useState();
const [stock_name, setStock] = React.useState();
const axios = require('axios');
var data = JSON.stringify({
"ticker": stock_name
})
try{
let response = await axios({
method: 'post',
url:'http://0.0.0.0:8000/predict',
headers: {
'accept' : 'application/json',
'Content-Type' : 'application/json'
},
data: data
},
)
if (response.status==200){
console.log(response.data);
setPred(response.data);
}
return {predData}
}
catch(error){
console.log(error);
}
}
This is the FastAPI function in my python backend:
#app.post("/predict")
def prediction(payload: StockIn):
stock_name = payload.stock_name
pred = stock_prediction(stock_name)
a = int(pred)
response_object = {"data" : a }
return response_object
Can anyone help me out here?
First of all, you send {'ticker': '...'}.
And in backend you try to access a property called stock_name.
Most likely this is caused by it.
If that doesn't help, check below.
It seems that your situation is related with your backend, so you can edit the header.
You can check the variables by simply adding
print statements after important parts.
For example;
#app.post("/predict")
def prediction(payload: StockIn):
print('Payload:', payload)
stock_name = payload.stock_name
pred = stock_prediction(stock_name)
print('Pred:', pred)
a = int(pred)
response_object = {"data" : a }
return response_object
NOTE: It's about backend because axios doesn't interfere with the response codes.

AWS lambda response ERROR: string indices must be integers (Textract)

I'm new to this and have spent 3 weeks on this error. I'm trying to get the response from a lambda function of the extracted text in an image in an s3 bucket. I successfully upload the image into the bucket from me expo project using this code:
try {
const response = await fetch(data.uri);
const blob = await response.blob();
const assetType = 'Mobile Device';
await Storage.put(`${username}/${assetType}/${UUID}`, blob, {
contentType: 'image/jpeg',
progressCallback,
});
} catch (err) {
console.log("Error uploading file:", err);
}
this will invoke the lambda function to extract the needed text
import json
import boto3
from urllib.parse import unquote_plus
import re
def lambda_handler(event, context):
bad_chars = [';', ':', '!', "*", ']', "[" ,'"', "{" , "}" , "'",","]
file_obj = event["Records"][0]
bucketname = str(file_obj["s3"]["bucket"]["name"])
filename = unquote_plus(str(file_obj["s3"]["object"]["key"], encoding='utf-8'))
textract = boto3.client('textract', region_name='ap-south-1')
print(f"Bucket: {bucketname} ::: Key: {filename}")
response = textract.detect_document_text(
Document={
'S3Object': {
"Bucket": bucketname,
"Name": filename,
}
}
)
result=[]
processedResult=""
for item in response["Blocks"]:
if item["BlockType"] == "WORD":
result.append(item["Text"])
element = item["Text"] + " "
processedResult += element
print (processedResult)
res=[]
imei=""
#Extracting imei number and removing the bad characters
str_IMEI= str(re.findall('(?i)imei*?[1]*?[:]\s*\d{15}',processedResult))
imei= str(re.findall('[^\]]+',str_IMEI))
imei = str(imei.split(' ')[-1])
for i in bad_chars :
imei = imei.replace(i, '')
if len(imei)>0 :
res.append(str(imei))
else:
res.append('')
#Extracting imei2 number and removing the bad characters
str_IMEI2= str(re.findall('(?i)imei*?[2]*?[:]\s*\d{15}',processedResult))
imei2= str(re.findall('[^\]]+',str_IMEI2))
imei2 = str(imei2.split(' ')[-1])
for i in bad_chars :
imei2 = imei2.replace(i, '')
if len(imei2)>0 :
res.append(str(imei2))
else:
res.append('')
#Extracting model number and removing the bad characters
str_Model = str(re.findall('(?i)model*?[:]\s*[\w|\d|_,-|A-Za-z]+\s*[\w|\d|_,-|A-Za-z]+',processedResult))
result = str(str_Model.split(' ')[-1])
for bad_char in bad_chars :
result = result.replace(bad_char, '')
if len(result) >0 :
res.append(result)
else:
res.append('')
print(res)
return {
'statusCode': 200,
#'body': JSON.stringify(result)
'body': res
}
and then use RESTapi to get the response from the lambda
try{
const targetImage = UUID + '.jpg';
const response = await fetch(
'https://6mpfri01z0.execute-api.eu-west-2.amazonaws.com/dev',
{
method: 'POST',
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(targetImage)
}
)
const OCRBody = await response.json();
console.log('OCRBody', OCRBody);
} catch (err) {
console.log("Error extracting details:", err);
}
but i keep getting this error
OCRBody Object {
"errorMessage": "string indices must be integers",
"errorType": "TypeError",
"requestId": "8042d6f7-44de-4a54-8209-fe93ca8570ec",
"stackTrace": Array [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler
file_obj = event[\"Records\"][0]
",
],
}
PLEASE HELP.
As the error says, you are trying to slice a string type with a some kind of key as if it were a dict type:
>>> str_name = 'Jhon Doe'
>>> str_name['name']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
Normaly, this issue could cause, that you do not receive the correct data in header.
In all of my Lambda function, is use something like this:
bucket = event['Records'][0]['s3']['bucket']['name']
Event is most of the time a valid dict within Python, so you have two possibilities here:
Option 1
Before proceeding, to something like
if 'Records' in event:
if 's3' in event['Records'][0]:
if 'bucket' in ...
if 'name' in ...
Only if all condition are true, you should proceed.
Option 1
do a simple
print(event)
and check your event, when your Lambda will be failing.
The issue here could cause that the Lambda is triggered before the S3 object is available.
What you can do is to add a S3 trigger which will be invoked automatically the Lambda, as soon as the S3 object was uploaded. See here
https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-event-types-and-destinations.html

C# HttpWebRequest Exeption Cookies

Hi I'm trying to convert a python code to C#. My Problem is, that I have a problem to make a HttpWebRequest. I don't get the cookies for the POST Request.
The Website response is 404. That is correct. But I think, I need the cookies for the POST request.
Is it correctly transleted? I have not done a HttpWebRequest yet.
C# Code:
public string email = "test#example.com"
public string password = "123456"
public string client_id = "111111111111"
public string login_url = "https://account.komoot.com/v1/signin"
var headers = new Dictionary<string, string> {
{"Content-Type", "application/json"}};
var payload = new Dictionary<string, string> {
{"email", email},
{"password", password},
{"reason", "null"}};
try
{
var request = (HttpWebRequest)WebRequest.Create(login_url);
request.CookieContainer = new CookieContainer();
var response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
var exeptionResponse = ex.Response;
var cookies = exeptionResponse ["Cookies"];
throw new WebException();
}
try
{
var request = (HttpWebRequest)WebRequest.Create(login_url);
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/json";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write(payload);
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
Console.WriteLine(result);
}
}
catch (Exception exe)
{
throw new Exception();
}
Python Code:
import requests
import json
email = "test#example.com"
password = "123456"
client_id = "111111111111"
login_url = "https://account.komoot.com/v1/signin"
tour_url = f"https://www.komoot.de/user/{client_id}/tours"
s = requests.Session()
res = requests.get(login_url)
cookies = res.cookies.get_dict()
headers = {
"Content-Type": "application/json"
}
payload = json.dumps({
"email": email,
"password": password,
"reason": "null"
})
s.post(login_url,
headers=headers,
data=payload,
cookies=cookies,
)
url = "https://account.komoot.com/actions/transfer?type=signin"
s.get(url)
headers = {"onlyprops": "true"}
response = s.get(tour_url, headers=headers)
if response.status_code != 200:
print("Something went wrong...")
exit(1)
data = response.json()
tours = data["user"]["_embedded"]["tours"]["_embedded"]["items"]
for idx in range(len(tours)):
print(f"({idx+1}) {tours[idx]['name']}")
tour_nr = int(input("Tour ID: "))
tour_nr -= 1
tour_url = tours[tour_nr]["_links"]["coordinates"]["href"]
response = s.get(tour_url, headers=headers)
tour_data = json.loads(response.text)
tour = tours[tour_nr]
tour['coordinates'] = tour_data
print("Title:", T.name())
print(f"Duration: {T.duration()}s")
Tanks for your help!
Try it
public async Task<string> HttpClientAsync(string json, string token)
{
try
{
var JsonData = new StringContent(json, Encoding.UTF8, "application/json");
var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
};
using (var client = new HttpClient(handler))
{
// System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var result = await client.PostAsync(url, JsonData);
string resultContent = await result.Content.ReadAsStringAsync();
return resultContent;
}
}
catch (Exception e)
{
return e.Message;
}
}

Python to GAS translation not working: get bad request error

I want the following code to be translated into GAS from python. I wrote the GAS version pasted below but it is not working. It must be something simple but I don't know the reason why I get this error. Any advice will be appreciated. Thanks.
import requests
requestId = "*******************"
url = "http://myapi/internal/ocr/"+requestid+"/ng"
payload={}
headers = {
'X-Authorization': 'abcdefghijklmn'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I wrote this at the moment but I get bad request error.
function sending(yesorno, requestId) {
var requestId = "*******************"
 var STAGING_KEY = "abcdefghijklmn"
var url = url = "http://myapi/internal/ocr/"+requestId+"/ng"
var data = {}
var options = {
'muteHttpExceptions': true,
'method': 'post',
'payload': JSON.stringify(data),
'headers': {
'X-Authorization': STAGING_KEY
}
};
//Error processing
try {
var response = JSON.parse(UrlFetchApp.fetch(url, options));
if (response && response["id"]) {
return 'sent';
} else {
//reportError("Invalid response: " + JSON.stringify(response));
//return 'error';
Logger.log('error')
}
} catch (e) {
//reportError(e.toString());
//return 'error';
Logger.log('error')
}
}
Modified Code
function sending() {
var requestId = "*************************"
var STAGING_KEY = "abcdefghijklmn"
var url = "http://myapi/internal/ocr/"+requestId+"/ng";
var data = {}
var options = {
'muteHttpExceptions': true,
'method': 'post',
'payload': data,
'headers': {
'X-Authorization': STAGING_KEY
}
};
try {
var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
Logger.log(response)
if (response && response["id"]) {
return 'sent';
} else {
//reportError("Invalid response: " + JSON.stringify(response));
//return 'error';
Logger.log('error1')
}
} catch (e) {
//reportError(e.toString());
//return 'error';
Logger.log('error2: '+ e.toString())
}
}
Error
error2: Exception: Bad request:
I understood your situation as follows.
Your python script works fine.
You want to convert the python script to Google Apps Script.
When your Google Apps Script is run, an error Exception: Bad request: occurs.
In this case, how about the following modification? When response = requests.request("POST", url, headers=headers, data=payload) is used with payload={}, I think that at Google Apps Script, it's 'payload': {}.
Modified script:
function sending() {
var requestId = "*******************"
var STAGING_KEY = "abcdefghijklmn"
var url = "http://myapi/internal/ocr/" + requestId + "/ng"
var data = {}
var options = {
'muteHttpExceptions': true,
'method': 'post',
'payload': data,
'headers': {
'X-Authorization': STAGING_KEY
}
};
try {
var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
console.log(response)
if (response && response["id"]) {
return 'sent';
} else {
//reportError("Invalid response: " + JSON.stringify(response));
//return 'error';
Logger.log('error')
}
} catch (e) {
//reportError(e.toString());
//return 'error';
Logger.log('error')
}
}
Note:
By the above modification, the request of Google Apps Script is the same as that of the python script. But if an error occurs, please check the URL and your STAGING_KEY, again. And, please check whether the API you want to use can access from the Google side.
Reference:
fetch(url, params)

Categories

Resources