0

Using redux-api-middleware which works similarly to axios and jquery.ajax, I passed a formData which is a mixture of an image and other form values as you can see on this image: enter image description here

The problem I have is that after successfully calling the API via a POST request, the PHP $_POST object is null though there was an actual POST request happening. This is my code snippet:

import { CALL_API } from "redux-api-middleware";

export function createTestAnnouncement(data) {
  return (dispatch, getState) => {
    const { auth: { oauthToken, oauthTokenSecret } } = getState();

    const formData = new FormData();

    Object.entries(data).forEach(([key, value]) => {
      if (key === 'image') {
        formData.append(key, value);
      } else {
        formData.set(key, value);
      }
    });

    return dispatch({
      [CALL_API]: {
        endpoint: "/api/test-announcements",
        method: "POST",
        headers: {
          'xoauthtoken': oauthToken,
          'xoauthtokensecret': oauthTokenSecret,
        },
        body: formData,
        types: [CREATE_TEST_ANNOUNCEMENTS, CREATE_TEST_ANNOUNCEMENTS_SUCCESS, CREATE_TEST_ANNOUNCEMENTS_FAILURE]
      }
    })
  }
}

How will be able to get values from the $_POST object? Did I use the FormData object correctly?

EDIT: My Controller is just this, PS: I am sure this working because this is working on a plain application/json request

use api\controllers\BaseController;
use model\Operations\TestAnnouncements\TestAnnouncementOperation;
use model\DB\TestAnnouncement;

class IndexController extends BaseController
    public function actionCreate()
    {
        var_dump($_POST);
        // Commented this out because the payload is not JSON
        // $request = \Yii::app()->request;

        // $op = new TestAnnouncementOperation();
        // $op->topic = $request->getJSON('topic');
        ...
    }
...

I always get a NULL on my var_dump. While using postman and passing form-data on the body generates me a value on my $_POST.

The Bassman
  • 2,241
  • 5
  • 26
  • 38
  • There is no PHP code. – Pinke Helga Mar 07 '19 at 04:37
  • Where is your server side of code? Did it works properly? If you send a post request using tool such as postman or curl, did your php server get the data? – hcheung Mar 07 '19 at 04:39
  • I tried using postman, the $_POST object from the API was not null but when the request is from my JS app, it's NULL. – The Bassman Mar 07 '19 at 05:02
  • If you are using Yii framework to do rest, you should use their request class to obtain params from client better than raw code. eg. `$arrival = yii::$app->request->post('arrival');`. then try and test with your postman if you can get payload from postman request. I assume that you have a problem with ` redux-api-middleware` module. you can check this 'https://www.yiiframework.com/doc/api/2.0/yii-web-multipartformdataparser' also. – Sen Sokha Mar 07 '19 at 05:45
  • @SenSokha thank you for the answer sir but I'm actually using Yii 1.. I'll have to include it on the tags. – The Bassman Mar 07 '19 at 05:53
  • @SenSokha I actually used this: `$yii::$app()->request->getPost('topic') //topic is the payload from JS`, it returned to NULL while it looks good on PostMan. The primary reason why I didn't post the PHP code to this at first because I was actually thinking there is no problem with the PHP code. I wanted people to examine if there is something wrong with my JS code. – The Bassman Mar 07 '19 at 06:05
  • Check what Content-Type header actually gets send, in your browser dev tools network panel. – 04FS Mar 07 '19 at 08:11
  • hello @04FS, it's multipart/form-data with a boundary that's automatically added by the browser. – The Bassman Mar 07 '19 at 09:35
  • Sounds like that is as it should be then. Does it show the individual parameters as well in there? (Section titled “Form Data” in Chrome dev tools.) – 04FS Mar 07 '19 at 10:24
  • Yes! Form data is there, and that's the puzzling thing.. – The Bassman Mar 07 '19 at 11:10

2 Answers2

0

you can check if the variable is set or not using...

if(!isset($_POST["ur_varible_name_from_html_form"]))
{

  echo "error";
}
0

You can see redux-api-middleware repo on github, issues #125. They already have resolved and given example [CALL_API] and [RSAA] with from data.

Community
  • 1
  • 1
Sen Sokha
  • 1,784
  • 16
  • 16