[Developers] Problems uploading images
Coral Link
corallink at gmail.com
Sun Jan 6 11:40:12 UTC 2008
Hello FreeBDevs:
So we finally got over the hump here; we fired up 'tcpflow' (awesome network
diagnostic tool, by the way, easier than tcpdump) to monitor the exchange
between the local system and freebase.
-The Broken-
Freebase's 'raw' upload url (sandbox.freebase.com/api/service/upload) only
accepts 'POST' requests but doesn't know how to deal with
multipart/form-data as a content type. PHP/libcurl defaults its POST to
Content-Type: multipart/form-data, and embeds the image inside of that
multipart envelope. Below is the tcpflow output with the broken code, minus
some extraneous stuff (192.168.0.150 is the local system, 208.068.108.121 is
sandbox.freebase.com):
192.168.000.150.46220-208.068.108.121.00080: POST /api/service/upload
HTTP/1.1
Content-Type: image/gif
Content-Length: 13365
Content-Length: 13555
Expect: 100-continue
192.168.000.150.46220-208.068.108.121.00080: Content-Type:
multipart/form-data; boundary=----------------------------b8245f93a0cb
------------------------------b8245f93a0cb
Content-Disposition: form-data; name="filename"; filename="CORAL.gif"
Content-Type: image/gif
192.168.000.150.46220-208.068.108.121.00080:
GIF89a+.........).X.T.r....-.w.\.r....1{..d>3o;..5wV.l~3..,g.....;<Image
Data Here>
192.168.000.150.46220-208.068.108.121.00080:
------------------------------b8245f93a0cb--
208.068.108.121.00080-192.168.000.150.46220: HTTP/1.0 400 Bad Request
***This lead to the cryptic "cannot identify image file" response from the
freebase server.***
-The Fix-
Straightforward, but there are no examples of this I could find in Google to
use CURLOPT_READDATA instead of CURLOPT_POSTFIELDS:
Replace one line:
curl_setopt($request, CURLOPT_POSTFIELDS,$content);
With these two lines, dropping the @ from the front of the file name:
$fp = fopen ($content, "r");
curl_setopt($request, CURLOPT_READDATA, $fp);
-The New-
The working interaction is as follows, note there is no mention of
multiparts:
192.168.000.150.59161-208.068.108.121.00080: POST /api/service/upload
HTTP/1.1
Content-Type: image/gif
Content-Length: 13365
Expect: 100-continue
192.168.000.150.59161-208.068.108.121.00080: GIF89a+............,.<Image
Data Here>
\.r....1{..d>3o;..5wV.l~3..,g.....;
208.068.108.121.00080-192.168.000.150.59161: HTTP/1.0 200 OK
Hope this leads to less pulled out hair for someone.
Regards,
Indy
On Dec 30, 2007 2:05 AM, Coral Link <corallink at gmail.com> wrote:
> Hi; I'm using PHP/LibCURL to upload images relevant to my topics. I'm
> guessing its more of a PHP issue than Freebase, but hopefully someone will
> indulge a helping hand.
>
> I'm running into this problem when uploading an image:
>
> { "status": "400 Application Error", "code": "/api/status/error",
> "messages": [ { "info": { "exception": "cannot identify image file" },
> "message": "Invalid image file.", "code":
> "/api/status/error/upload/invalid_image_data" } ] }
>
> When I run the following:
>
> $content="@/hole/www/corallink.com/freebase/files/uwlImage/9202a8c04000641f80000000060b61a7/CORAL.gif";
> $type="image/gif";
>
> $request = curl_init($url);
> curl_setopt($request, CURLOPT_COOKIEFILE, $cookiejarfile); //get some
> cookies
> curl_setopt($request, CURLOPT_POST, 1);
> curl_setopt($request, CURLOPT_HTTPHEADER, array("X-Metaweb-Request: TRUE",
> "Content-Type: ".$type));
> curl_setopt($request, CURLOPT_POSTFIELDS,$content);
>
> // Return the result instead of printing it out.
> curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
> curl_setopt($request, CURLOPT_COOKIE, $credentials);
>
> // Now fetch the URL
> $responsetext = curl_exec($request);
> curl_close($request);
>
>
> This same code works great for uploading documents for the description in
> /common/topic. I've tried variations where I've made
> $content=array("file"=>"@ filename.gif"), which uploaded data, but
> Freebase tagged it as media_type 'octet/stream'. Any suggestions will be
> appreciated.
>
> Thank you and kind regards,
> Indy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freebase.com/pipermail/developers/attachments/20080106/a9159e4c/attachment-0001.htm
More information about the Developers
mailing list