node request vs python session - python

I have a working node.js script which written using the node.js request module.
I'm trying to convert this script to python with the session module.
I'm new to python and I followed the python docs as it mentioned. but I'm struggling to get my code works.
the problem I'm having is sending the cookie values in the subsequent requests with the session module.
as per the docs it is saving cookies and send them automatically in any requests after that. but
here is my working node.js script
const request = require('request');
const fs = require('fs');
const getOptions = {
jar:true,
followAllRedirects:true,
method:'GET',
url:'https://dummyurl.com'
};
request.get(getOptions,(err,response,html)=>{
if(err){
console.log('error in request');
console.log(err);
}
else {
const postOptions = {
jar:true,
followAllRedirects: true,
method:'POST',
url:'https://dummyurl.com',
form:{
'data':{
'page':2
}
}
};
request.post(postOptions,(err,response,html)=>{
if(err){
console.log('post err');
console.log(err);
}
else {
fs.writeFileSync('pyres.html',html,'utf8');
}
})
}
});
this is my python conversion of above script
s = requests.Session()
url= 'https://dummyurl.com'
response = s.get(url)
print(response.cookies)
data_url = 'https://dummyurl.com/'
postData = {
"data":{
"page":2
}
}
resultResponse = s.post(data_url,data=postData)
print(resultResponse.content)
Can anyone points me out any mistake in this code?

actually the problem was in data format.
in nodejs I post it like this
{'data':{'page':2} }
but in python it should be converted like this
{
'data[page]': '2'
}
not sure why it was not worked in normal json format in python

Related

How to fetch data analyzed in python to node.js and pass it to angular?

I am new to angular and i want to display JSON data from python to angular with the help of node.js and I used child process to connect python and node.js but I dont know how to pass it to angular service
node.js file
const express = require('express')
const { spawn } = require('child_process')
const app = express()
const port = 8000
app.get('/', (req, res) => {
let dataToSend
let largeDataSet = []
// spawn new child process to call the python script
const python = spawn('python', ['test.py'])
// collect data from script
python.stdout.on('data', function (data) {
console.log('Pipe data from python script ...')
//dataToSend = data;
largeDataSet.push(data)
})
// in close event we are sure that stream is from child process is closed
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`)
// send data to browser
res.send(largeDataSet.join(''))
})
})
app.listen(port, () => {
console.log(`App listening on port ${port}!`)
})
Technically you just have to send a Http GET request from your service.
I suggest that you should read and follow this offical http client guide to set it up correctly.
Here is a simple service snippet. This should be enough.
#Injectable({
providedIn: 'root',
})
export class MyService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
const url = '';
return this.http.get(url);
}
}

Swift HTTP session not sending actual Request

So I have some Swift code that send a request to my local host
//
// ContentView.swift
// Shared
//
// Created by Ulto4 on 10/23/21.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button(action : {
self.fu()
}, label: {
Image(systemName: "pencil").resizable().aspectRatio(contentMode:.fit)
})
}
}
func fu(){
let url = URL(string: "http://127.0.0.1:5000/232")
guard let requestUrl = url else { fatalError() }
var request = URLRequest(url: requestUrl)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error took place \(error)")
return
}
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
However, on my Flask app there are no get requests coming in and the function isn't running. There also isn't anything printing to the console.
I am fairly new to swift so I don't really know how to fix this.
Is there any other way to send requests in swift, if not, How would I fix this?
You are creating the URLSessionDataTask, but you never start it. Call task.resume(), e.g.
func performRequest() {
guard let url = URL(string: "http://127.0.0.1:5000/232") else {
fatalError()
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Error took place \(error)")
return
}
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
}
task.resume() // you must call this to start the task
}
That having been said, a few caveats:
You are doing http rather than https. Make sure to temporarily enable insecure network requests with app transport settings, e.g.
You didn’t say if this was for macOS or iOS.
If running on physical iOS device, it will not find your macOS web server at 127.0.0.1 (i.e., it will not find a web server running on your iPhone). You will want to specify the IP number for your web server on your LAN.
If macOS, make sure to enable outbound network requests in the target’s “capabilities”:
You asked:
Is there any other way to send requests in swift?
It is probably beyond the scope of your question, but longer term, when using SwiftUI, you might consider using Combine, e.g., dataTaskPublisher. When running a simple “what was the status code” routine, the difference is immaterial, but when you get into more complicated scenarios where you have to parse and process the responses, Combine is more consistent with SwiftUI’s declarative patterns.
Let us consider a more complicated example where you need to parse JSON responses. For illustrative purposes, below I am testing with httpbin.org, which echos whatever parameters you send. And I illustrate the use of dataTaskPublisher and how it can be used with functional chaining patterns to get out of the mess of hairy imperative code:
struct SampleObject: Decodable {
let value: String
}
struct HttpBinResponse<T: Decodable>: Decodable {
let args: T
}
class RequestService: ObservableObject {
var request: AnyCancellable?
let decoder = JSONDecoder()
#Published var status: String = "Not started yet"
func startRequest() {
request = createRequest().sink { completion in
print("completed")
} receiveValue: { [weak self] object in
self?.status = "Received " + object.value
}
}
func createRequest() -> AnyPublisher<SampleObject, Error>{
var components = URLComponents(string: "https://httpbin.org/get")
components?.queryItems = [URLQueryItem(name: "value", value: "foo")]
guard let url = components?.url else {
fatalError("Unable to build URL")
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: HttpBinResponse<SampleObject>.self, decoder: decoder)
.map(\.args)
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
struct ContentView: View {
#ObservedObject var requestService = RequestService()
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button {
requestService.startRequest()
} label: {
Image(systemName: "pencil").resizable().aspectRatio(contentMode:.fit)
}
Text(requestService.status)
}
}
}
But, like I said, it is beyond the scope of this question. You might want to make sure you get comfortable with SwiftUI and basic URLSession programming patterns (e.g., making sure you resume any tasks you create). Once you have that mastered, you can come back to Combine to write elegant networking code.
FWIW, like workingdog said, you could also use the new async-await rendition of data(for:delegate:). But when in the declarative world of SwiftUI, I would suggest Combine.

HttpResponseMessage returns 403 in .NET

I have a async method which make a HttpClient call repeatedly in .Net. I simulate my code as a small console app below:
private static HttpClient req { get; set; } = new HttpClient();
static async Task Main(string[] args)
{
Console.WriteLine("Please press enter to start healthCheck");
Console.ReadLine();
healthCheck();
Console.ReadLine();
}
private static async Task healthCheck()
{
while (true)
{
req.DefaultRequestHeaders.Add("apikey", "myPassword");
string strUrl = "http://myUrl";
HttpResponseMessage hrm = await req.GetAsync(strUrl);
Console.WriteLine("=> statusCode:" + (int)hrm.StatusCode);
await Task.Delay(5000);
}
}
The output is:
The problem is when I use Postman or write this code with python as below, every time it responded 200 instead of 403.
import requests as req
import time as t
url = "http://adpsms.adpdigital.com/report/?date=2021-08-30"
customHeader = {"apikey": "sssrjdIiGisbViKA"}
i = 10
while (i > 0):
response = req.get(url, headers = customHeader)
print("statusCode: " + str(response.status_code))
i -= 1
t.sleep(5)
I supposed it is a server error but when I responded 200 every time with python I understand it would be a problem with my code or something client based.
Since my project is based on .NET I want to make it work on it.
Any suggestion would be appreciated.
On every iteration of your loop, you are adding DefaultRequestHeaders.
It means that they will be added again and again on each iteration of your cycle to the global instance of HttpClient
According to the official docs these headers will be sent with each request.
For your particular task you might add them only once(that's why the name contains prefix default.)
So if you slightly rewrite your code like this:
private static HttpClient req { get; set; } = new HttpClient();
static async Task Main(string[] args)
{
Console.WriteLine("Please press enter to start healthCheck");
Console.ReadLine();
AddDefaultHeaders();
healthCheck();
Console.ReadLine();
}
private static void AddDefaultHeaders()
{
req.DefaultRequestHeaders.Add("apiKey", "myPassword");
}
private static async Task healthCheck()
{
while (true)
{
string strUrl = "http://myUrl";
HttpResponseMessage hrm = await req.GetAsync(strUrl);
Console.WriteLine("=> statusCode:" + (int)hrm.StatusCode);
await Task.Delay(5000);
}
}
It should works fine.

How to interface to web-based predictive endpoints using Python

The following web-page provides example code for using a predictive endpoint using c#
https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CVSPredictionSample
{
public static class Program
{
public static void Main()
{
Console.Write("Enter image file path: ");
string imageFilePath = Console.ReadLine();
MakePredictionRequest(imageFilePath).Wait();
Console.WriteLine("\n\nHit ENTER to exit...");
Console.ReadLine();
}
public static async Task MakePredictionRequest(string imageFilePath)
{
var client = new HttpClient();
// Request headers - replace this example key with your valid Prediction-Key.
client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");
// Prediction URL - replace this example URL with your valid Prediction URL.
string url = "<Your prediction URL>";
HttpResponseMessage response;
// Request body. Try this sample with a locally stored image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(url, content);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
private static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
How can it be done using Python code ?
Many thanks
A similar documentation exists for Python:
For classification projects here
For object detection projects here
See language selector in the page:

Alamofire 5 multipart image upload issue swift for flask API

I was trying to upload single image via Alamofire 5 multipart data, API is working fine on Postman as well as on android side, but it is not working on iOS side.
API is developed in Python flask. Image is taken from camera and by using JPEGCompression uploading image.
Following is my code:
func postMultipartData(imageData: Data, completion:#escaping (Result<AccuracyModel?, ErrorResponse>) -> Void) {
let url = APIConstant.ImageAccuracyBaseUrl.BASEURL
let mimeType = "image/jpeg"
let headers: HTTPHeaders = [
"Content-Type": ContentType.multipart.rawValue
]
AF.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData, withName: "file", fileName: "file123.jpg", mimeType: mimeType)
print(multipartFormData.boundary)
}, to: url, usingThreshold: UInt64.init(),
method: .post,
headers: headers).response { response in
switch response.result {
case .success(_):
if response.response?.statusCode == 200 || response.response?.statusCode == 201 {
do {
if let data = response.data {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
print(json ?? "")
let decodedData = try! JSONDecoder().decode(AccuracyModel.self, from: data)
DispatchQueue.main.async {
completion(.success(decodedData))
}
} else {
print(response)
}
} catch {
completion(Result.failure(self.generateErroModel()!))
}
} else if response.response?.statusCode == 500 {
completion(Result.failure(self.generateErroModel()!))
} else {
fallthrough
}
break
case .failure(_):
completion(Result.failure(self.generateErroModel()!))
}
}
}
For testing purpose api is using 5000 port, can that be issue?
There are no parameters required, so not sending any.
I have also tried by using NSURLSession, but no luck.
For Flask code, I have referred following link:
https://pytorch.org/tutorials/intermediate/flask_rest_api_tutorial.html
Thanks in Advance.

Categories

Resources