I'm using the HttpSocket request method of CakePHP to perform some requests to some api. I want to send some parameters with my request. Does anybody know how I can send some parameters using that method? For example, let's say I'm sending a request to this url:
http://www.mydomain.com
but I wanna send parameter to this request for example:
username: smith123
password: qwerty
If I were to do this with Ajax, I would do something like:
$.post('http://www.mydomain.com', {username: "smith123", password: "qwerty"}, 'json');
How do I send those parameters (also the type of data expected as specified in the ajax example above) to http://www.mydomain.com using the request method of the HttpSocket class of CakePHP
Please help
Thank you
I think you would want to use the get or post method, not request, which is the base method.
http://book.cakephp.org/1.3/en/view/1518/get
You can pass parameters in the second argument of the get method as either a string or an array:
App::import('Core', 'HttpSocket');
$HttpSocket = new HttpSocket();
$results = $HttpSocket->get('http://www.google.com/search', 'q=cakephp');
debug($HttpSocket->response);
Related
I can not get values sent from post method, using http request.
I am getting values using get method, but I need to get it using post method.
I am not using any view, I want to call http url, and send some data in my controller using post method.
This is how my controller looks like,
namespace Spaarg\eMenuApi\Controller\Index;
class Products extends \Magento\Framework\App\Action\Action
{
public function __construct(\Magento\Framework\App\Action\Context $context)
{
return parent::__construct($context);
}
public function execute()
{
//$token = $this->getRequest()->getPostValue();
$token = $this->getRequest()->getPost();
}
}
I am new to magento 2, and I don't understand what is the problem.
It will be great if someone can help.
It probably has to do with the Content-type of the http request, where Magento only understands Json and Xml (this is explained here). If you're using a different Content-type in the request or your data doesn't match the type declared in the header, then getPost() will not work.
As a fallback, you can always get all the POST data by using the following way:
public function execute()
{
$postData = file_get_contents("php://input");
}
Keep in mind that this will get the raw string, so you will likely need to process it accordingly before using it (for example with json_decode() or something like that).
For more information about this, check this SO question.
I can access request params using Request::input() or Request::all().
The problem is that my request includes both GET and POST params, but only GET ones are used to calculate signature.
Is there a way to retrieve only a set of GET or a set of POST params from request in Laravel 5.1?
Or going with $_GET and $_POST is my only option here?
Thank you.
You can use Request::query() to get only GET parameters. Keep in mind that there are no guaranties about consistency in the order of parameters you get from GET, so you might need to sort the array before calculating the signature - depending on how you calculate the signature.
If you need something straightforward you can just use the global helper:
$pathData = request()->path(); <br />
$queryData = request()->query(); <br />
$postData = array_diff(request()->all(), request()->query());
https://laravel.com/docs/5.6/requests
Follow these instructions to extend the Laravel Request class with your own:
https://stackoverflow.com/a/30840179/517371
Then, in your own Request class, copy the input() method from Illuminate\Http\Request and remove + $this->query->all():
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all();
return data_get($input, $key, $default);
}
Bingo! Now in a POST request, Request::query() returns the query (URL) parameters, while Request::input() only returns parameters from the form / multipart / JSON / whatever input source.
I'm following Album tutorial on Zend Framework 2's website, and I want to send object that have been converted to Json and pass controller via AJAX, I can invoke the method on the controller as well as receive Json from controller, but I don't know how to send parameter to it.
Thanks for reading and sorry for my bad English.
Do you mean pushing parameters to your controller via AJAX, like this?
$.post('/your/url', { value: val, row: row, element: element }, //parameters
function (ret) {
});
I tried to pass parameters from URL, from a post form; $request->getMethod() always returns (GET), and it's not even returning the values.
$request = new \Zend\Http\Request();
echo $request->getMethod(); // GET
print_r($request->getPost('name')); // null
Note: name is the input field's name from the post form.
You should use PhpEnvironment Request class which also derived from Zend\Http\Request like stated by #Xerkus.
The ancestor Http Request class is useless in your case, it used by various classes (for example Zend\Http\Client) internally when dealing with HTTP protocol.
Try this:
$req = new \Zend\Http\PhpEnvironment\Request();
echo $req->getMethod();
I'm creating an API using Cakephp 2.x that needs a POST request to post some data to the server however when I'm posting (using Postman) to 127.0.0.1/appname/api/confirm with code=123 in the post parameters my $_POST is an empty array.
My route works, I can see variables that I declare and output within the controller, and I've checked that the parameters are being passed in the request by using the chrome developer console checking the network data.
Router::connect('/api/confirm', array('controller' => 'awesomeController', 'action' => 'confirm'));
<?php
class AwesomeController extends AppController {
public function confirm() {
$this->autoRender = false;
$this->layout = 'ajax';
pr($_POST);
}
}
?>
I've got my endpoints for the get requests to work just fine, it only seems to be POST data.
Not quite sure why $_POST wouldn't even be available and I'm sure it's something ridiculously silly I've overlooked!
** Edit **
I've attempted the following without success:
$this->request->query
$this->request->data
$this->request->params
I have another method whereby I use GET along with ?parameter=value etc and I am able to use one of the above calls to retrieve the data.
In this case, the variables should be in
$this->request->query
Try using URLs like api/confirm?code=123, and they will be in request->query
I may be wrong since I am pretty new to cakePHP but since you set:
$this->autoRender = false;
so the view is not rendered automatically to set the view to ajax layout.
Isn't it necessary to call:
$this->render();
After setting the layout as said here?
Well, hope it helps.
If anybody come here one day by googling, just had the same problem.
Had a REST Controller, called with URL /rest/something/cool.json
Method called inside RestController.php, had output, but no POST, no REQUEST.
Tried with code=123, sending direct JSON, the only way to make it works was to set Content-Type to application/json and to send actual working JSON : Cake seems to validate prior to anything, sending raw data seems useless.