0

First of all, I looked at this question and tried the solution, did not work for me.

I am working in Zend 1.12 on a PHP 7.0.22 server.

I have a function that should post data to an API and receive a confirmation of the post.
Whenever I test it in my rest client, in firefox, I get this response from the API call:
"{\"code\":400,\"text\":\"Missing required parameters.\"}".
When I add a header: Content-Type: application/x-www-form-urlencoded I get the expected respone: '200, true'. But even when trying to set this header in my code explicitly, it still always returns 400.

Controller:

$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john@example.com';

$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setParameterPost( $params );
$response = client->request( Zend_Http_Client::POST );
var_dump( $response );

I have tried to work with raw data aswell..

$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john@example.com';
$params = json_encode( $params );

$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setRawData( $params, 'application/json' );
$response = client->request( Zend_Http_Client::POST );
var_dump( json_decode( $response ) );

When I try to dump the $request with the post parameters inside my API (becuase I wrote the API myself), I see that this is null. Like there is no $post being sent or no data is actually being passed. Because the error 'missing required parameter' is untrue, all the parameters are set ( but maybe not passed / sent correctly ).

Can somebody tell me what I am missing? I have been at this all day. Thanks!

EDIT (@shevron):
The API expects the parameters to be passed via url-encoded-form. Setting the appropriate header in the REST Client Content-Type: application/x-www-form-urlencoded makes the API return a good response. When this header is not added, I get the error.

nclsvh
  • 2,628
  • 5
  • 30
  • 52
  • 1
    What content does your API expect? It's not clear whether you expect data as JSON or as URL-encoded parameters (e.g. in PHP's `$_POST`). – shevron Jun 09 '18 at 12:01
  • College wrote the API, but I see the code checks `$request->isPost()`. So I am guessing the API expects a PHP Post? This also makes sense, because when adding the header `Content-Type: application/x-www-form-urlencoded` in the REST Client, the api returns an answer as expected. – nclsvh Jun 11 '18 at 06:46
  • 1
    ZF should set the header to `x-www-form-urlencoded` by default for POST requests. Since this is your own API I'd suggest checking what the content type of the request is set to when you API receives it. – Tim Fountain Jun 11 '18 at 13:47

0 Answers0