Problem sending POST data to my controller

Discussion forum for Extension Writers regarding Extension Development.
Juanla
Registered User
Posts: 54
Joined: Mon Aug 24, 2009 8:03 pm

Problem sending POST data to my controller

Post by Juanla »

Hello everyone.

I am making my first extension, to test some extension development features.

I was trying send data from a view to one controller. I can get thsi using Get and LamedParameters in the path. But When I try make this using post never read my params. I will try show you my problem (I hope you can help me with this)

I make a POST request like this:

Code: Select all

http://localhost:8000/app.php/genaut/translate
Request Method: POST
Request Payload: {"text":"This is an example post in your phpBB3 installation. Everything seems to be working. You may delete this post if you like and continue to set up your board.","lang_target":"es"}
I have this routing config (Route without parameters because in Post method are not neccessary):

Code: Select all

genaut_posttranslator_route:
    path: /genaut/translate
    defaults: { _controller: genaut.posttranslator.controller.translator_controller:translate}
    methods: [POST]
In my controller "translate" method, I have this:

Code: Select all

public function translate($text, $lang_target)
{

    $translation_response = $text;
    if(empty($text) || empty($lang_target)){
			$translation_response = 'Cant\'t translate (Not received info). Original text:' . $text;
    }
    
}
I am getting this error:

Code: Select all

{
message : "Falta el valor para el argumento #1: <strong>text</strong> en la clase <strong>genaut\\posttranslator\\controller\\translator_controller:translate</strong>"
}
Before, I tested with a GET configuration like this (or something like this) and worked fine:

Code: Select all

genaut_posttranslator_route:
    path: /genaut/translate/{text}/{lang_target}
    defaults: { _controller: genaut.posttranslator.controller.translator_controller:translate}
So I think that I am missing something in the routing. I tried too using request class to get the post variables using "is_set_post" and ohter functions but nothing retrieve me data. Any idea?
Sorry my bad english :)
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28851
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier

Re: Problem sending POST data to my controller

Post by Paul »

phpBB doesnt parse the json automatically that is send via POST. You will need to get the raw POST data and use json_decode on that data
Juanla
Registered User
Posts: 54
Joined: Mon Aug 24, 2009 8:03 pm

Re: Problem sending POST data to my controller

Post by Juanla »

What is the default post data that I should recover here (in the XXX)? There are a default name?

Code: Select all

$this->request->is_set_post('XXX')
Sorry my bad english :)
User avatar
Toxyy
Registered User
Posts: 964
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek

Re: Problem sending POST data to my controller

Post by Toxyy »

Juanla wrote: Mon Apr 01, 2024 10:45 pm What is the default post data that I should recover here (in the XXX)? There are a default name?
See this page, particularly the variable section:
https://area51.phpbb.com/docs/dev/3.3.x ... l#variable

The name is the name of the variable you're sending via post, and the second field that you would put in is the default data if it is empty (usually 0 for integers, '' for strings), and the third parameter is if it is multibyte or not. See the docs on it here:
https://area51.phpbb.com/docs/code/3.3. ... d_variable
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

Some of my extensions:
[3.3][BETA] Post Form Templates || [3.3][BETA] Anonymous Posts || [3.2][3.3][BETA] ACP Merge Child Forums || [3.2][BETA] Sticky Ad || [3.2][DEV] User Delete Topics || [3.3][DEV] Moderate While Searching || [3.3][RC] Short Number Twig Extension
Juanla
Registered User
Posts: 54
Joined: Mon Aug 24, 2009 8:03 pm

Re: Problem sending POST data to my controller

Post by Juanla »

Thanks! I just sent the data from my ajax call in FormData format and can use it without problems. Looks like sending a JSON directly is not supported?

Anyway, thanks again
Sorry my bad english :)
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28851
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier

Re: Problem sending POST data to my controller

Post by Paul »

As said before, you will need to get the raw post data, and decode that yourself. You cant use the phpBB request class as that requires you sending normal post data.
Juanla
Registered User
Posts: 54
Joined: Mon Aug 24, 2009 8:03 pm

Re: Problem sending POST data to my controller

Post by Juanla »

Paul wrote: Tue Apr 09, 2024 3:51 pm As said before, you will need to get the raw post data, and decode that yourself. You cant use the phpBB request class as that requires you sending normal post data.
Yep, I made this using normal post data and working fine. Thanks
Sorry my bad english :)

Return to “Extension Writers Discussion”