HTTP POST a python file using dart - python

I want to send a python file via http post in dart. I can do it in CURL the following way:
curl -X POST -F 'file=#/home/user/file.py' http://192.168.80.1:9888/dir/file.py
I am also able to do it in python like this:
import requests
url = 'http://192.168.80.1:9888/dir/file.py'
files = {'file': open('file.py', 'rb')}
print(files)
r = requests.post(url, files=files)
But in dart I am not able to send the post. I have tried several methods but is at this on currently:
import 'package:http/http.dart' as http;
import 'dart:io';
void main(List<String> arguments) async {
var response;
var file;
var url = 'http://192.168.80.1:9888/dir/file.py';
file = File('file.py').readAsStringSync();
var files = {'file': file};
response = await http.post(url, body: files);
}
Which result in the following exception: Exception has occurred.
ClientException (Connection closed before full header was received)
I know that the server is working due to CURL and python. How do I mimic the functionality in CURL/python using dart?

I was able to send the python file via a POST using dio.
import 'package:dio/dio.dart' as dio;
Future<dio.FormData> FormData3() async {
return dio.FormData.fromMap({
'file': await dio.MultipartFile.fromFile(
'files/file.py',
filename: 'file.py',
),
});
}
Future<dio.Response> sendFile() async {
dio.Response response;
response = await dio.Dio().post('http://192.168.80.1:9888/dir/file.py',
data: await FormData2(), onSendProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + '%');
}
},
options: dio.Options(
method: 'POST',
responseType: dio.ResponseType.plain,
));
return response;
}
void main() async {
response = await sendFile();
}

Related

C# POST request from C# asp.net to python

I try to convert a working c# asp.net Post Request to python code but can#t define the json post request right. Here the working C# example:
public async Task Run()
{
var request = new ExternalExecutionRequest()
{
ConfigPath = Path.Combine(_destinationDir, "Export.json"),
TemplatesPath = _destinationDir,
FilesPaths = new List<string>() { Path.Combine(_destinationDir, "export_xls.xlsx")
},
OutputPath = Path.Combine(_destinationDir, "OutputRest"),
Compress = true
};
var httpClient = new HttpClient();
var response = await httpClient.PostAsJsonAsync($"http://localhost:63155/Login", "c:\\lizenz.lic");
request.Token = await response.Content.ReadAsAsync<string>();
await httpClient.PostAsJsonAsync($"http://localhost:63155/AnalyseRequest", request);
await httpClient.PostAsJsonAsync($"http://localhost:63155/Logout", "");
}
I tried different functions with the librarys request or because it is an asynchronous request with grequests. But i can't even name the variables right in the json request.
license_path = "c:\\lizenz.lic"
license_json = {"value": license_path}
response = grequests.post("http://localhost:63155/Login", json=license_json)
Could someone help me to convert the working request to python?

Fetch JSON.parse: unexpected end of data at line 1 column 1 of the JSON data fast api

Hi i got a python fast api backend server.
#app.get("/getName")
async def getName() -> JSONResponse:
data_sniffer = DataSniffer()
data = json.dumps(data_sniffer.get_data())
print(data)
return JSONResponse(content=data,headers={'content-type':'application/json'})
this is the console output of data :{"first_name": "test"}
My Frontend Code looks like this
useEffect(() => {
async function query() {
const options = {
method: 'GET',
mode: 'no-cors',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application-json',
},
};
const res = await fetch('http://127.0.0.1:3000/getName', options);
console.log(res);
const data = await res.json();
console.log(data);
}
query();
});
but i get following error message SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
my network response looks like this:
"{"first_name": "test"}"
Has anyone a clue why fetch cant parse the json?
For anyone who has the same issue what worked for me is I moved my Backend folder to the root directory of my Frontend and removed the mode no-cors of the fetch request. I installed the no cors app and now it works. I think the mode no-cors broke my request.

Performing file upload post request

I am trying to upload a file to a server, using python.
The API of the server has to accept the file, together with some other parameters.
This is why i open the file, and then i create a dictionary, containing the file and the other parameters that the web app accepts, and then encode it and perform a POST request with the item.
This is the code:
from urllib import request
from urllib.parse import urlencode
import json
with open('README.md', 'rb') as f:
upload_credentials = {
"file": f,
"descr": "testing",
"title": "READMEE.md",
"contentType": "text",
"editor": username,
}
url_for_upload = "" #here you place the upload URL
req = request.Request(url_for_upload, method="POST")
form_data = urlencode(upload_credentials)
form_data = form_data.encode()
response = request.urlopen(req, data=form_data)
http_status_code = response.getcode()
content = response.read()
print(http_status_code)
print(content)
However, i get an error in this line: response = request.urlopen(req, data=form_data)
The server responds with a 500 HTTP Status Code.
This is what i get as an error message:
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500:
500 error codes can mean a lot of things and i do not know how to progress any further, as everything i did seems by the book...
Does anyone have the experience, given the info provided here, to guide me to some potential solution?
EDIT: I am trying to replicate working js code that does the same thing. This is it:
<input type="file" />
<button onclick="upload()">Upload data</button>
<script>
upload = async() =>
{
const fileField = document.querySelector('input[type="file"]');
await uploadDoc(fileField.files[0] );
};
uploadDoc = async( file ) =>
{
let fd = new FormData();
fd.append( 'file', file );
fd.append( 'descr', 'demo_upload' );
fd.append( 'title', name );
fd.append( 'contentType', 'text' );
fd.append( 'editor', user );
let resp = await fetch( url, { method: 'POST', mode: 'cors', body: fd });
};
</script>
The js code is doing a multipart/form-data post request.
I do not believe urllib supports multipart/form-data, you can use request instead.
import requests
with open('README.md', 'rb') as f:
files = {"file": f}
upload_credentials = {
"descr": "testing",
"title": "READMEE.md",
"contentType": "text",
"editor": username,
}
r = requests.post("http://httpbin.org/post", files=files, data=upload_credentials)
print(r.status_code)
print(r.text)

Rest Api request in Node js (curl to Node.js, python to Node.js)

I have a curl code block that work succesfully.
curl -v -X POST "https://someapihost" \
-d "image_url=someimage.jpg" \
-H "Authorization: form1 key2"
And there is a python code block whick work exactly same.
import requests
headers = {
'Authorization': 'form1 key2',
}
data = {
'image_url': 'someimage.jpg'
}
response = requests.post('https://someapihost/', headers=headers, data=data)
But I need Node js code block which work samely, so I need to translate these code blocks.
Here is my translation. (I used https://curl.trillworks.com/#node)
var request = require('request');
var headers = {
'Authorization': 'form1 key2'
};
var dataString = 'image_url=someimage.jpg';
var options = {
url: 'https://someapihost',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
But the respose.statusCode return 400, so it does not work.
I wonder why this error occur and
how to currect this code.
(of course I install request by "npm install request")
I appreciate your reading this question.
Thanks
It depends on the api call you are making, but I would expect the following to work if it expects json as input.
var request = require('request');
var headers = {
'Authorization': 'form1 key2'
};
var data = {
image_url: 'someimage.jpg'
}
var options = {
url: 'https://someapihost',
method: 'POST',
headers: headers,
body: data,
json: true
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);

http post a base64 json request between Node.js and Python Flask server but Node received an incomplete json

I try to send a base64 json form node.js server to python flask server and than return a same base64 code back to node.js server. Flask can successfully receive my json but when it response to node and node try to print out the response. I got a error message say: "Unexpected end of JSON input". I found the reason is node server can not receive the base64 completely. It just only receive a small portion. What is the problem? Is post request has a string limit?
I tested when I change the base64 code to a short string. Node server can receive response normally.
Anyone can help me? Thank you.
This is my code:
<<< Node.js Server >>>
var express = require('express');
var http = require('http');
var app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(10000, () => console.log('Running on http://localhost:10000'));
postData = JSON.stringify({
'code': <base64 code or short string here>
});
var options = {
hostname: 'localhost',
port: 10001,
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(options, (res) => {
res.on('data', (chunk) => {
var data = JSON.parse(chunk);
console.log(data.message);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.write(postData);
req.end();
<<< Python Flask Server >>>
from flask import Flask
from flask import request
from flask import jsonify
app = Flask(__name__)
#app.route('/test', methods=['POST'])
def test():
request_data = request.get_json()
print(request_data['code'])
return jsonify(
message = request_data['code']
)
app.run(host='localhost', port=10001)
In NodeJS code, in data event, you will get chunk data(partial data), you need to wait until end event and then parse, following example may help you
var req = http.request(options, (res) => {
var data = '';
res.on('data', (chunk) => {
data += chunk.toString(); // buffer to string
});
res.on('end', () => {
data = JSON.parse(data);
console.log(data.message);
console.log('No more data in response.');
});
});

Categories