0

I created one internal api call as '/api/v1/user/promotional-emails' this api calls some external apis and we are updating data there. In postman api is working fine.

Now i created a user form as '/user/promotional-emails' this form is displaying finely but i have to call previously created api call '/api/v1/user/promotional-emails'. but it is always giving NULL as response.

I tried following method but it is not working. Don't know what needs to be done.

/**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    global $base_url;

    $result1 = $form_state->getValue('option1');
    $result2 = $form_state->getValue('option2');
    $result3 = $form_state->getValue('option3');
    $result4 = $form_state->getValue('option4');

    $json = [
        'headers' => [
          'Content-Type' => 'application/json',
        ],
          'form_params' => [
                  'EmailAddress' => 'test@gmail.com',
                  'EmailOptin' => true,
                  'option1' => $result1,
                  'option2' => $result2,
                  'option3' => $result3,
                  'option4' => $result4
              ]
            ];
    $options = ['option1' => $result1,'option2' => $result2,'option3' => $result3,'option4' => $result4, 'EmailAddress' => 'test@gmail.com'];

Option 1 I tried :

 $url = $base_url . '/api/v1/user/promotional-emails';

     $client = new Client(['verify' => false]);

     $rest_response = $client->request('POST', $url, $json);

This is also giving NULL

Option 2 I Tried

$client = \Drupal::service('http_client_factory')->fromOptions(array('verify' => FALSE));
    $request = $client->post($base_url.'/api/v1/user/promotional-emails', $options);
    $response = json_decode($request->getBody());

Here as well i am getting null. Any body can help me on this?

My API call function is like this.

public function unsubscribeEmail(Request $request) {
    // Get post body data in json.
    $requestContent = json_decode($request->getContent(), TRUE);
    // Get token.
    print_r($request); exit;
}
New contributor
Sravanthi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

Sravanthi is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.