Unable to post request successfully when I select raw json on Postman - python-3.x

I'm getting a bad request from postman when I make a request using raw json on Postman. The POST request accepts the following payload:
{"BusinessID": 34222222}
When I select form-data the request is successful.
I also copied the Python code from postman and executed it but I get a bad request.
Screenshot
The same applies when I try to run the code below.
query=99858585
url = "https://xxxxxxxxxxxxx.co.io/permit/api/checkBusiness"
payload = "{\"BusinessID\": query}"
headers = {
'Content-Type': "application/json",
'User-Agent': "PostmanRuntime/7.20.1",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "1bda2a65-6ed4-45d2-8029-5e70efa336ec,c1a967af-856e-4ab0-ad88-83b0b5c496df",
'Host': "xxxxxxxxxxxxxxxxx.co.io",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "23",
'Cookie': "ci_session=hnv0nsiksienscujctqmdm5mp2b0dp4q",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.json())
When I select form-data the request is successful
Headers after selecting form-data*

Related

Azure Function App Issue on running the code on VIA function APP

equest URL: 'https://analyticsdatatest.blob.core.windows.net/salesforcedata?restype=REDACTED&comp=REDACTED' Request method: 'GET' Request headers: 'x-ms-version': 'REDACTED' 'Accept': 'application/xml' 'User-Agent': 'azsdk-python-storage-blob/12.14.1 Python/3.8.16 (Linux-5.10.102.2-microsoft-standard-x86_64-with-glibc2.2.5)' 'x-ms-date': 'REDACTED' 'x-ms-client-request-id': 'fd778f22-a89hjhjkh---bjk----6f' 'Authorization': 'REDACTED' No body was attached to the request
Need solution on Azure finction APP

Python Requests - Getting {"status":300,"message":"Invalid request, no data received","data":null}

Website - https://imagerecognize.com/
This Site helps in identify objects in our image by using their Object Recognizer.
So, I would like to send a picture via python-requests and get the json data as follows:
{"status":200,"message":"Success","data":{"error":0,"type":"objects","objects":[{"name":"Car","confidence":98.35458374023438,"coordinates":[{"x":0.14399725198745728,"y":0.2877406179904938,"width":0.6706532835960388,"height":0.4653415083885193}]},{"name":"Automobile","confidence":98.35458374023438,"coordinates":[]},{"name":"Vehicle","confidence":98.35458374023438,"coordinates":[]},{"name":"Transportation","confidence":98.35458374023438,"coordinates":[]},{"name":"Sports Car","confidence":97.69086456298828,"coordinates":[]},{"name":"Jaguar Car","confidence":95.17160034179688,"coordinates":[]}],"quota":20}}
My Code:
headers = {
'accept-encoding': 'gzip, deflate, json',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryoeBDUD0Z6eOrmQBF',
'origin': 'https://imagerecognize.com',
'referer': 'https://imagerecognize.com/',
'user-agent': 'Mozilla/5.0',
'x-requested-with': 'XMLHttpRequest'
}
payload = {
'apikey': 'local',
"type": 'objects',
'min_confidence': '80',
'max_labels': '20',
'file': open('photo.jpg','rb'),
'token': '03AGdBq25OU7Ubu8WymeTg6BHCce27vEbOAV13osTIQwAqnnGAWD0YNXF0j097i-o5qb2-6UENkmjGZ14gZZUDOsq6B6f-dzHlPBkT_dijna7L1vXPMA_KRKKIJuk3rUTk-4Sszo14avemZ7MMPtUn1EF_tPd-IUu-r0mmhOtkMNIn_eLwU3uKFLSq-OYVF1FKaGD155ir9KGKVioAYMfsAyHPHP1grqnTVYEqt-_Zn-lbeuqMJI0UrKeQRGaY9SzGPl7x47gPrVdl3RPmanrM5o-JR3nD9UnL1BuZ-vpbfG-LIvYKg0GrZenK2qdT-PRIZbgKZ4yZ4hR5fDRzho8OkD444Y9B0o_PEexxpsABu9gulLBp0bsdwU9_tff80O3eWkHdw6pFErT7_LQrXR88Y5zU8kPY8jDnD2QhXD0E6xG6UOIKdXd1GBalhljO1pQDW5fQHyQLfStsl2RNZ_FGGs6eHjKC-_yXcSFQyWpzIewJc3NZJr8Lx-_wRuQbRuq7ztbPRQUEwEBd'
}
import requests
r = requests.post('https://imagerecognize.com/api/v3/', headers=headers, data=payload)
print(r.status_code)
print(r.json())
And this is the output:
300
{'status': 300, 'message': 'Invalid request, no data received', 'data': None}
I'd really appreciate all the help.

aws ec2 nodejs empty body sent through axios post request in react

I am deploying a MERN app on AWS EC2. Frontend is rendered perfectly on aws. While calling API(axios.post) empty body is received in backend when called through react frontend. But, when called through postman, the body is received correctly.
I am sharing the details of the request body as received on backend(I printed this for debugging purposes):
POSTMAN method:
Method: POST
Path: /api/auth
Body: { email_id: 'comiiii#gmail.com',
password: 'comiiii#123',
user_type: 'type_com' }
Headers: { host: '64.1.75.248',
'x-real-ip': '113.103.59.237',
'x-forwarded-for': '113.103.59.237',
'x-forwarded-host': 'ec2-64-1-75-248.ap-south-1.compute.amazonaws.com',
'content-type': 'application/json;charset=UTF-8',
connection: 'close',
'content-length': '94',
'user-agent': 'PostmanRuntime/7.26.8',
accept: '*/*',
'postman-token': 'a6ef6f75-716b-4843-a773-8b46d1f28427',
'accept-encoding': 'gzip, deflate, br' }
REACT FRONTEND:
Method: POST
Path: /api/auth
Body: {}
Headers: { host: '64.1.75.248',
'x-real-ip': '213.235.108.12',
'x-forwarded-for': '213.235.108.12',
'x-forwarded-host': 'ec2-64-1-75-248.ap-south-1.compute.amazonaws.com',
'content-type': 'application/json;charset=UTF-8',
connection: 'close',
accept: '*/*',
'access-control-request-method': 'POST',
'access-control-request-headers': 'access-control-allow-origin,content-type',
origin: 'http://ec2-64-1-75-248.ap-south-1.compute.amazonaws.com',
'user-agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36',
'sec-fetch-mode': 'cors',
referer: 'http://ec2-64-1-75-248.ap-south-1.compute.amazonaws.com/',
'accept-encoding': 'gzip, deflate',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8' }
I saw the difference between the two and realised that the headers which are different are part of forbidden ones. (Refer : https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name)
How to solve this?

Having trouble converting cURL POST command to python requests

I am trying to convert a block of cURL to python requests. I get the following error when I do:
{'error': 'invalid_request', 'error_description': 'request is missing a required parameter or malformed.'}
What am I translating incorrectly?
curl
POST /identity/v1/oauth2/token HTTP/1.1
Host: api.sandbox.ebay.com
Authorization: Basic <B64-encoded-oauth-credentials>
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=<URL-decoded-auth-code>&redirect_uri=<your_redirect_uri>
my_call.py
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
a 'Authorization': f'Basic {<Base64 encoded value>}'
}
data = {
'grant_type': 'authorization_code',
'code': client_id # str,
'redirect_uri': url # str,
'scope': 'https://api.ebay.com/oauth/api_scope/sell.inventory.readonly',
}
def get_oath_token():
url = 'https://api.ebay.com/identity/v1/oauth2/token'
r = requests.post(url, headers=headers, data=data)
print(r.json())

Scraping a page with node.js?

I want to get the flights table for every city from a page but befor that i have to set search fields and hit the button and befor that i have to import captcha at the first page. The web site designed with .Net. I import the captcha by my self but i want to do the rest with nodejs.
This is what i'm doing. first, import the captcha and then from loaded page and by looking at the firebug, set request header and request url and then send using http.request with GET method to scrape the page with nodejs again. after that i get viewstate of the page and again usign firebug, set another request header and "post data" and request url and send them using http.request to scrape the final page. This information that i set using firebug, are fixed; i mean, urls, header options and post data. what i have to change is city name in post data.
But i get a page with empty table. if is possible, What should i do? (sorry for my bad english :) )
This is the url (in persian): http://sepehr.iranhrc.ir.
Thanks in advance.
var http = require('follow-redirects').http;
var querystring = require('querystring');
var cheerio = require('cheerio');
var dataToAttach = {
'ScriptManager1': 'uplFlightSearch|btnSubmit37756070715319',
'__ASYNCPOST': true,
'__EVENTARGUMENT': '',
'__EVENTTARGET': '',
'__LASTFOCUS': '',
'__VIEWSTATE': '',
'__VIEWSTATEGENERATOR': 'E4CF65F9',
'btnSubmit37756070715319': '?????',
'dplFrom': 'THR',
'dplReservationRouteType': 'RoundTrip_FixedDate',
'dplTo': '0',
'dplFlightAdults': '1',
'dplFlightChilds': '0',
'dplFlightInfants': '0',
'txtCountUp': '00:26',
'txtDepartureDate': '1394/04/02',
'txtReturningDate': '1394/04/04'
};
var flightsSearchPageROs = {
hostname: 'sepehr.iranhrc.ir',
path: '/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?qry=sbV7wBdq4B7yEk1yv0OpvmofQkdkbWH49Wjk6UIMGiw95zDjDGo0/ssWJH8Wjv1D',
method: 'GET',
headers:{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Cookie': 'ASP.NET_SessionId=2iexj4pfxld4mdilfwttka2q;',
'Content-Type': 'text/html; charset=utf-8',
'Host': 'sepehr.iranhrc.ir',
'Referer': 'sepehr.iranhrc.ir'
}
};
var resultsPageROs = {
hostname: 'sepehr.iranhrc.ir',
path: '/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?action=display&rnd=2378726045210585',
method: 'POST',
headers:{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'ASP.NET_SessionId=o1ipad335qahuaahc25ngalr;',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Referer': 'http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx',
'Host': 'sepehr.iranhrc.ir',
'Pragma': 'no-cache',
'X-MicrosoftAjax': 'Delta=true',
'X-Requested-With': 'XMLHttpRequest'
}
};
var flightsSearchPage = http.request(flightsSearchPageROs, function(response{
var dataStream = '';
var htmlCode = '';
var date = '';
response.on('data', function(chunk){
dataStream += chunk;
});
response.on('end', function(){
htmlCode = cheerio.load(dataStream);
secondDataToAttach.__VIEWSTATE = htmlCode("__VIEWSTATE").val();
resultsPageROs.headers['Content-Length'] = querystring.stringify(dataToAttach).length;
resultsPageRequest();
});
});
flightsSearchPage.on('error', function(e){console.log("Error0: " + e.message);});
flightsSearchPage.end();
function resultsPageRequest(){
var changingCitiesBoxResponse = http.request(resultsPageROs, function(response){
response.setEncoding('utf8');
var dataStream = '';
var htmlCode = '';
response.on('data', function(chunk){
dataStream += chunk;
});
response.on('end', function(){
htmlCode = cheerio.load(dataStream);
console.log(htmlCode.html());
});
console.log('STATUS: ' + response.statusCode);
});
changingCitiesBoxResponse.on('error', function(e){console.log("Error1: " + e.message);});
changingCitiesBoxResponse.end(querystring.stringify(dataToAttach));
}
EDIT
One thing that i forgot to mention it was that, this is done in PHP with Curl and i looked at the code in curl part and done exactly like that with node and http.request. PHP returns correct answer but mine not.
And I used phantom as well.This is the code:
var url = "http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?action=display&rnd=4565721642440773";
var settings = {
operation: "POST",
encoding: "utf8",
weak: false,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'ASP.NET_SessionId=2iexj4pfxld4mdilfwttka2q;',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Referer': 'http://sepehr.iranhrc.ir/Systems/FA/Reservation/Flight_NewReservation_Search.aspx?qry=sbV7wBdq4B7yEk1yv0OpvmofQkdkbWH49Wjk6UIMGiw95zDjDGo0/ssWJH8Wjv1D',
'Host': 'sepehr.iranhrc.ir',
'Pragma': 'no-cache',
'X-MicrosoftAjax': 'Delta=true',
'X-Requested-With': 'XMLHttpRequest'
},
data: querystring.stringify(secondDataToAttach)
};
phantom.create(function (ph){
ph.createPage(function (page){
page.open(url2, settings, function(status){
console.log(status);
page.evaluate(function(){ return document.body.innerHTML; }, function(result){
console.log('Content ' + result);
ph.exit();
});
});
});
}, {dnodeOpts: {weak: false}
});
But i didn't get the answer.
And yes, it uses AJAX to send the request. According to Firebug:
The first request is sending a post data and the second one, i think is a redirect that gives the link to the result page(i used that link but no luck) and the third is the result page with the flight details that i can't get.
May be i used them wrong. How can i use these Three request to get flights table?
I think what's happening is that the page you want the results from is loading them through a separate AJAX request, and a regular get request isn't going to get that. You'll need to either figure out what that separate request is and do it yourself, or scrape the page with something like PhantomJS that can execute the javascript for the page.
First though, go to the page on your browser regularly and watch the network tab and locate that request that loads in the data.

Resources