Skip to content

form_params not working #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thewebsitedev opened this issue Sep 25, 2019 · 4 comments
Closed

form_params not working #803

thewebsitedev opened this issue Sep 25, 2019 · 4 comments

Comments

@thewebsitedev
Copy link

Hi there,

Thank you for this library!

I am trying to make a post request where I need to send a couple of parameters as well. But the parameters are not being included with the request.

Working CURL request:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'aid=...&api_token=...&user_token=...' 'https://mywebsite.com/api/subscription/list'

Not Working Request:

$request = $provider->getAuthenticatedRequest(
    'POST',
    'https://mywebsite.com/api/subscription/list',
    $existingAccessToken->getToken(),
    [
        'form_params' => [
            'aid'        => '...',
            'api_token'  => '...',
            'user_token' => $existingAccessToken->getToken()
        ]
    ]
);

Error Message:

array(4) {
  ["code"]=>
  int(400)
  ["ts"]=>
  int(1569391222)
  ["validation_errors"]=>
  array(1) {
    ["message"]=>
    string(19) "aid cannot be empty"
  }
  ["message"]=>
  string(30) "Validation errors, see details"
}

What did I miss?

@Bibendus83
Copy link

I have the same issue, form_params is completely ignored!

@Bibendus83
Copy link

Bibendus83 commented Mar 13, 2020

Ok, I finally found a solution.
This library doesn't support the form_params option but only headers, body and version as you can see here:

public function getRequestWithOptions($method, $uri, array $options = [])
{
$options = $this->parseOptions($options);
return $this->getRequest(
$method,
$uri,
$options['headers'],
$options['body'],
$options['version']
);
}

The only way is to work manually with the body option.
Here is my solution that I posted on StackOverflow
https://stackoverflow.com/questions/52718933/making-post-request-with-thephpleague-oauth2-client/60674068#60674068

$postData = [
    'date' => $date,
    'service_ids' => $serviceId,
];
$options = [
    'body' => http_build_query($postData),
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
];

$request = $this->provider->getAuthenticatedRequest("POST", $requestUrl, $token, $options);
$response = $this->provider->getParsedResponse($request);

Some said that it should work using json_encode() instead of http_build_query() and application/json in the Content-Type but I was not able to make it work that way.

@dpi
Copy link

dpi commented Oct 23, 2020

Created #863 to expand on this

@ramsey
Copy link
Contributor

ramsey commented Oct 28, 2020

Closing since there is a work-around. We'll treat #863 as a feature request. Thanks!

@ramsey ramsey closed this as completed Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants