Is is possible to create and send a similar HTTP-request using the POST-method in JavaScript?
POST register.jsp HTTP/1.1
Host: hi/iq
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,
video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1
Accept-Language: en-us, en;q=0.50
Accept-Encoding: gzip, deflate, compress;q=0.9
Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------29772313742745
Content-Length: 452
-----------------------------29772313742745
Content-Disposition: form-data; name="name"
J.Doe
-----------------------------29772313742745
Content-Disposition: form-data; name="email"
abuse#spamcop.com
-----------------------------29772313742745
Content-Disposition: form-data; name="file-upload"; filename="test.txt"
Content-Type: text/plain
test data with some high ascii: ¿Como estás?
-----------------------------29772313742745--
The most important things I need are in the last 13 lines (form text data + form file data).
The data (text+file) are to be retrieved in a PHP file serverside.
I have already tried using XMLHttpRequest and Formdata, but I can't get it working properly.
The attempt that makes most sense imo is:
var fd3 = new FormData();
fd3.append('mode', 'end'); // set $_POST['mode']="end"
fd3.append('file', image.jpg); // set $_FILES['file']=image.jpg
var xhr3 = new XMLHttpRequest();
xhr3.open("POST", "pages/upload_arc3d.php", false);
xhr3.setRequestHeader("Content-type", "multipart/form-data");
xhr3.send(fd3);
Related
I just started programming but I don't know how to get this POST response into a PHP file,
Request URL: http://localhost/getEmployees.php
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:63342
Referrer Policy: no-referrer-when-downgrade
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:63342
content-length: 4850
Content-type: text/html; charset=UTF-8
server: PhpStorm 2020.1
vary: origin
X-Powered-By: PHP/7.3.6
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,es-US;q=0.8,es;q=0.7,sm;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 105
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: Phpstorm-7e92279c=4f7ff7c6-1a41-4c9e-90dd-dcc6680dcb42; loginAuthorised=loginAuthorised; companyId=1; userId=10; userName=Claudia+Najera; PHPSESSID=nequ49622cdstv6oh0ahsmis10
Host: localhost:63342
Origin: http://localhost:63342
Pragma: no-cache
Referer: http://localhost:63342/htdocs/flavorite.io/tools/tests.php?_ijt=buaa7pmm8icavombffnis1htfu
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36
X-Requested-With: XMLHttpRequest
query[generalSearch]: Mario
selectedAllRows: false
requestIds: true
sort[field]:
sort[sort]: asc
What I am trying to do is get query[generalSearch] string. I have in another PHP file to fet the results:
$dbConnection = connection();
$generalSearchJSON = #$_POST['generalSearch'];
$generalSearch = json_decode($generalSearchJSON);
I want to use the $generalSearch variable to search the database. But it doesn't seem to get the result. Any suggestions?
You have a typo.Remove the # from $_POST['generalSearch'];
So you'll get this:
$dbConnection = connection();
$generalSearchJSON = $_POST['generalSearch'];
$generalSearch = json_decode($generalSearchJSON);
I want to get get the contents from a webpage but the user sees different information depending if he is logged or not. I want to send header information with curl to simulate that the use is logged in.
I inspected the network and these are the response headers:
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:close
Content-Type:text/html
Date:Tue, 13 Jun 2017 08:08:52 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Location:http://dims-92.com/ClientNewsPage
Pragma:no-cache
Server:Apache/2.2.3 (CentOS)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.30
And there is this request payload:
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="SubmitControlId"
Auto_CAuthenticate_LogIn_LogIn_Standart
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="ParameterInfo"
undefined
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CEShop_SearchControl_SearchInput"
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_UsernameInput"
user
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_PasswordInput"
password
------WebKitFormBoundaryaSWkHLJeD9EymCJb--
I have tried this:
$url = "http://dims-92.com/ClientDisplayProductFolder?param=4553686f703a434e493d3935343b434e494c3d3b5649443d3b543d42473b";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryaSWkHLJeD9EymCJb',
'Content-Length: 671',
'------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="SubmitControlId"
Auto_CAuthenticate_LogIn_LogIn_Standart
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="ParameterInfo"
undefined
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CEShop_SearchControl_SearchInput"
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_UsernameInput"
user
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_PasswordInput"
password
------WebKitFormBoundaryaSWkHLJeD9EymCJb--'
));
$content = curl_exec($ch);
echo $content;
but the page I see just says: Bad Request
Your browser sent a request that this server could not understand.
Request header field is missing ':' separator.
------WebKitFormBoundaryaSWkHLJeD9EymCJb
You can't post headers like this, they have to be in an array like so :
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data',
'Content-Length: 671',
'Content-Disposition: form-data',
....
));
The problem is that you post the entire payload (things like boundary=----WebKitFormBoundaryaSWkHLJeD9EymCJb'), which is invalid.
your code is confusing the requests' HTTP HEADERS and the HTTP BODY
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Length: 671
these are parts of the HTTP REQUEST HEADERS, and indeed goes into the CURLOPT_HTTPHEADER.
Content-Disposition: form-data; name="SubmitControlId"
Auto_CAuthenticate_LogIn_LogIn_Standart
------WebKitFormBoundaryaSWkHLJeD9EymCJb
Content-Disposition: form-data; name="ParameterInfo"
undefined
this is part of the HTTP REQUEST BODY, the body does not go in CURLOPT_HTTPHEADER.
now, contrary to what Julien Lachal says in https://stackoverflow.com/a/44517070/1067003 , you can actually encode the entire request body yourself (using CURLOPT_POST or CURLOPT_INFILE), but when using multipart/form-data or application/x-www-form-urlencoded encoding, its easier, safer, and less error prone, to let curl encode it for you. (the usual reason for encoding it yourself, is when POSTing to a JSON API requiring content-type: application/json, curl does not support encoding to JSON automatically.)
to tell curl to do it for you, simply use CURLOPT_POST and CURLOPT_POSTFIELDS, like this:
curl_setopt_array ( $ch, array (
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array (
'SubmitControlId' => 'Auto_CAuthenticate_LogIn_LogIn_Standart',
'ParameterInfo' => 'undefined',
'FC_CEShop_SearchControl_SearchInput' => '',
'FC_CAuthenticate_LogIn_UsernameInput' => 'user',
'FC_CAuthenticate_LogIn_PasswordInput' => 'password'
)
) );
now libcurl will automatically multipart/form-data-encode it, and set the correct content-type, and set the correct content-length header, and the actual HTTP request will look like:
Http Request Headers:
POST / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Content-Length: 686
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------b6890d3827808ee1
Http Request Body:
--------------------------b6890d3827808ee1
Content-Disposition: form-data; name="SubmitControlId"
Auto_CAuthenticate_LogIn_LogIn_Standart
--------------------------b6890d3827808ee1
Content-Disposition: form-data; name="ParameterInfo"
undefined
--------------------------b6890d3827808ee1
Content-Disposition: form-data; name="FC_CEShop_SearchControl_SearchInput"
--------------------------b6890d3827808ee1
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_UsernameInput"
user
--------------------------b6890d3827808ee1
Content-Disposition: form-data; name="FC_CAuthenticate_LogIn_PasswordInput"
password
--------------------------b6890d3827808ee1--
however, note that many websites doesn't support multipart/form-data and/or prefer application/x-www-form-urlencoded encoding. to use that, use http_build_query on the data to CURLOPT_POSTFIELDS, like this:
curl_setopt_array ( $ch, array (
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query ( array (
'SubmitControlId' => 'Auto_CAuthenticate_LogIn_LogIn_Standart',
'ParameterInfo' => 'undefined',
'FC_CEShop_SearchControl_SearchInput' => '',
'FC_CAuthenticate_LogIn_UsernameInput' => 'user',
'FC_CAuthenticate_LogIn_PasswordInput' => 'password'
) ),
CURLOPT_URL => 'http://127.0.0.1:8080'
) );
now the actual HTTP request looks like this:
HTTP request headers:
POST / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Content-Length: 204
Content-Type: application/x-www-form-urlencoded
HTTP request body:
SubmitControlId=Auto_CAuthenticate_LogIn_LogIn_Standart&ParameterInfo=undefined&FC_CEShop_SearchControl_SearchInput=&FC_CAuthenticate_LogIn_UsernameInput=user&FC_CAuthenticate_LogIn_PasswordInput=password
I have HTTP PUT API but cannot retrieve from the request. Input::all() returns empty. Can you share how to do it? I confirmed that $request had form-data.
public function update(Request $request, $id)
{
Log::info(Input::all()); // empty
Log::info($id); // 1001
Log::info($request);
}
$request ->
local.INFO: PUT /v1/shops/1001 HTTP/1.1
.
.
.
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="item_id"
9001
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="name"
tokyo
Route.php
<?php
Route::resource('v1/shops', 'ShopController');
?>
Detail
$request ->
local.INFO: PUT /v1/shops/1001 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: ja,en;q=0.8,en-US;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 332
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAchDYDABHI1GDUeu
Cookie: XXXXXXXXXXXXXX
Host: XXXXXXXXXXXXXX
Origin: chrome-extension://aicmkgpgakddgnaphhhpliifpcfhicfo
Postman-Token: 434976e3-3476-3dcd-3689-7048d523dd20
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="item_id"
9001
------WebKitFormBoundary2B26VoCplFAB8W36
Content-Disposition: form-data; name="name"
tokyo
I found it. I should have selected "x-xxx-form-urlencoded". not "form-data"
I got this form data and could retrieve by Input::all()
user_id=1&address=2&tag=3
This issue a was clue. https://github.com/postmanlabs/postman-app-support/issues/8
I am using simpleTest WebBrowser for DataScraping on this URL http://www.magicbricks.com/bricks/agentSearch.html. But though everything seems right I always get the error City Field is required. I guess the problem might be with the fact that values in city field changes dynamically when value of State changes. Any solutions? Here is my code.
<?php
require_once('simpletest/browser.php');
$browser = &new SimpleBrowser();
$browser->addHeader('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2');
$browser->get('http://www.magicbricks.com/bricks/agentSearch.html');
$browser->setField('source','agentSearch');
$browser->setField('_transactionType','1');
$browser->setField('_propertyType','1');
$browser->setField('resultPerPage','50');
$browser->setField('agentSearchType','B');
$browser->setField('state','520');
$browser->setField('city','4320');
$browser->setField('keyword','');
$browser->setField('country','50');
print $browser->submitFormById('searchFormBean');
print $browser->getResponseCode()
?>
Here are some errors i noticed
Field Missing
Missing agentSearchType field
Missing transactionType ( There is Both transactionType & _transactionType)
missing propertyType ( There is both propertyType & _propertyType)
There are some header information you need to add such as
Referer
Cookie
A typical post test should come this format if you view the headers
POST http://www.magicbricks.com/bricks/agentSearch.html HTTP/1.1
Host: www.magicbricks.com
Connection: keep-alive
Content-Length: 173
Cache-Control: max-age=0
Origin: http://www.magicbricks.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.magicbricks.com/bricks/agentSearch.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=nF1UqV3DM2tZC42zByYm6Q**.MBAPP09; __utma=163479907.1423216630.1331970312.1331970312.1331970312.1; __utmb=163479907.1.10.1331970312; __utmc=163479907; __utmz=163479907.1331970312.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _mbRunstats=3k0ilrpcgprh4tea
source=agentSearch&agentSearchType=B&country=51&state=601&city=8417&transactionType=11951&_transactionType=1&propertyType=10001&_propertyType=1&keyword=tesy&resultPerPage=50
I hope this helps
:D
i have next problem
$("#btnsave").click(function () {
$.post('svld.php', {
'fnd': $('#fnd').attr("value")
}, function (data) {
alert(data);
}, 'json');
}
slvd.php
header('Content-type: application/json');
var_dump(json_encode($_POST));
in request i see next
fnd http://ya.ru
and have great response
string(24) "{"fnd":"http:\/\/ya.ru"}"
but alert(data) not runed
if i do same request on file parse.php in same folder
if($_POST['fnd']){
header('Content-type: application/json');
echo json_encode($cntTags->returnArrayTags());
}
all work perfect
Have idea what it can be ?
header list
Date Thu, 08 Jul 2010 12:16:02 GMT
Server Apache/2.2.14 (Win32) PHP/5.3.0
X-Powered-By PHP/5.3.0
Content-Length 38
Keep-Alive timeout=5, max=99
Connection Keep-Alive
Content-Type application/json
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept application/json, text/javascript, */*
Accept-Language ru,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://localhost/tz/
Content-Length 22
Cookie spylog_test=1
The issue is that your HTTP request is returning this string:
string(24) "{"fnd":"http:\/\/ya.ru"}"
What happens is that jQuery is trying to parse that but it fails because it's not a valid JSON string.
Try using echo instead of var_dump:
echo json_encode($_POST);
var_dump() outputs other information that you don't need, and that will screw up jQuery's JSON parsing.
Content-Type application/json Заголовки запросапоказать исходный код
looks alien to me. Looks like a character-set failure.
try
header('Content-type: application/json; charset=UTF-8');