DeepLcom / deepl-php

Official PHP library for the DeepL language translation API.
MIT License
202 stars 23 forks source link

Problems with POST request with getUsage (400 status code - bad request) #20

Closed sypets closed 1 year ago

sypets commented 1 year ago

I had some problems using this client library. Specifically, theTranslator::getUsage() call worked on a development system, but not on other machine (with the same software package). Everything else worked very reliably.

The problem that occurs is that the getUsage request returns 400 HTTP status code (bad request).

I can reproduce this consistently, I even created a small test script with (basically) the same code.

After some tests I could narrow it down:

All systems use

Always worked:

Problems on some machines:


The problems occured on:

Ok:

Suspected problem

Resproduce

It is a little difficult to reproduce because the problem does not manifest itself on all systems and I am not sure what is the relevant setting.

Possible solution

Ideally, figure out exact reason why server sends 404. However, this also seems to fix the problem:

  1. Either use GET with getUsage. I am not sure why POST is used, the API docs use GET: https://www.deepl.com/docs-api/general/get-usage/
  2. or

in HttpClient::sendRequest

$headers['Content-Type'] = 'application/json';
$payload = json_encode( [] );
$curlOptions[\CURLOPT_POSTFIELDS] = $payload;

Additional information

I activated logging and also have a test script which reproduces the problem on the affected machines and outputs some more information. I can upload the logs if requested.

The test script is here: https://github.com/sypets/sypets_test_deepl_usage_standalone

JanEbbing commented 1 year ago

Thanks for reporting this. getUsage should use a GET request, as per the API docs. I am fixing this in all client libraries. (commit with the fix)

seekuehe commented 1 year ago

Reopened due to no release tag yet

JanEbbing commented 1 year ago

Released a new version 1.4.0 that includes this fix, among some other changes, see CHANGELOG.md