Page MenuHomePhabricator

ERR_INVALID_REQ when trying upload a file with API
Closed, InvalidPublic

Description

See this, too: http://lists.wikimedia.org/pipermail/wikitech-l/2011-January/050958.html

When I try to upload a file with the API, I get this error:

Request: [unknown method] [no URL], from <my IP> via
amssq35.esams.wikimedia.org (squid/2.7.STABLE7) to [unknown host]
([unknown])
Error: ERR_INVALID_REQ, errno [No Error] at Sun, 02 Jan 2011 09:43:01 GMT

The source code of the program, you can get at: http://svn.toolserver.org/svnroot/p_commonshelper2/truck/ . The request is in the method upload() in the upload_class.php.


Version: unspecified
Severity: major
OS: Windows Vista
Platform: PC

Details

Reference
bz26624

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 11:15 PM
bzimport set Reference to bz26624.
bzimport added a subscriber: Unknown Object (MLST).

unknown method, no URL, and ERR_INVALID_REQ all make me suspect that there's a basic problem with your HTTP request itself: the squid proxy servers which are Wikimedia's first line of response are giving you a complete rejection, saying it can't understand what you're telling it even enough to send it to the actual web servers.

It looks like you're manually constructing an HTTP request from scratch by mashing strings together. Make absolutely sure that you're doing this correctly, for instance:

  • are all the variables correct?
  • are their values actually getting interpolated?
  • do you have any issues with mixtures of \r\n and \n? (\r\n should be used at the line separator, I believe, but unless your code is changing it you'll have *either* \r\n or \n depending on your OS's file encoding...?

You might for instance try dumping the string and checking it manually before sending it out. If you get the same, try making your request simpler -- take out some of those parameters and see what you get. Confirm that you can make an HTTP request _at all_ with this code, then add parameters back until you see what's wrong.

However in general, I'd recommend that you go ahead and use some existing HTTP library that can handle constructing an HTTP request with multipart form submission data, such as CURL or the PEAR HTTP_Request2 module.

soxred93 wrote:

I am having this same problem, using the regular cURL class in PHP. The relevant code: http://code.google.com/p/mw-peachy/source/browse/trunk/Plugins/image.php?r=330#415

(In reply to comment #2)

I am having this same problem, using the regular cURL class in PHP. The
relevant code:
http://code.google.com/p/mw-peachy/source/browse/trunk/Plugins/image.php?r=330#415

Got an exact sequence of commands to reproduce the issue with your code?

soxred93 wrote:

Not exactly, it's kind of complex. It also happens about 1 in 100 times, so it's not easily reproducable. You can get an idea of what API params I'm using.

It sounded like the original poster's problem is *every* time rather than 1% of hits, so this likely isn't the same issue.

1% of the time is more likely to involve occasional bad processing or bugs on the server end, especially if resubmitting the same request works. That'd be something that needs to be worked out with server admins.

I test the request with my local Apache server and there are no problems. I can read out the parameters ($_POST[...]).

My problem is reproducable with other input parameters.

The line endings are \r\n, because the file is created in Windows and my text editor show no mixtures.

Closing this since a reproducible test case hasn't been provided. If you want to re-open it, please provide a test case so that we can reproduce the problem.

PHP-Script to show error message

Here is a test file for the problem which works with curl.

Attached:

You are not using the curl library correctly.

If you pass $message to as the value for CURLOPT_POSTFIELDS instead of $query_data it will work.

curl cannot be expected to translate arbitrary PHP data structures into usable POST data.

I recommend you use a HTTP client library (there are a few mentioned in http://willnorris.com/2009/03/http-client-library-for-php or you can try MediaWiki's own Http class (found in HttpFunctions.php).