3

I want to upload a jpg file to imgur and get the jpg's link.

I have imgur API's Client Id and Client Secret.

Delphi code as below:

procedure TfrmMain.Button6Click(Sender: TObject);
var
  client: TRESTClient;
  request: TRESTRequest;
  response: TCustomRESTResponse;
begin
  client := TRESTClient.Create(nil);
  try
    client.BaseURL := 'https://api.imgur.com/';
    Client.AddParameter('Client ID', '...', TRESTRequestParameterKind.pkHTTPHEADER);
    Client.AddParameter('Client Secret', '...', TRESTRequestParameterKind.pkHTTPHEADER);
    request := TRESTRequest.Create(nil);
    try
      request.Client := client;
      request.Method := rmPOST;
      request.Resource := 'a/C11W7xC';
      request.Accept := 'application/json';
      request.AddParameter('image','D:\linedw.jpg' , pkFile);
      request.Execute;
      response := request.Response;
      if response.Status.Success then
      begin
        mo_response.Lines.add('Success: ' + slinebreak + response.Content);
      end
      else
      begin
          mo_response.Lines.add('Failed ' +  response.StatusText + ': ' + slinebreak + response.Content);
      end;
    finally
      request.Free;
    end;
  finally
   client.Free;
  end;
end;

The error information from response.Content is as below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>imgur: the simple 404 page</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="robots" content="noindex,nofollow" />
    <meta name="keywords" content="images, funny pictures, image host, image upload, image sharing, image resize" />
    <meta name="description" content="Imgur is home to the web's most popular image content, curated in real time by a dedicated community through commenting, voting and sharing." />
    <meta name="copyright" content="Copyright 2014 Imgur, Inc." />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge;" />
    <link rel="stylesheet" type="text/css" href="https://s.imgur.com/min/404.css?1393899213" />
    <!--[if IE 9]><link rel="stylesheet" href="https://s.imgur.com/include/css/ie-sucks.css?0" type="text/css" /><![endif]-->
</head>
<body>
    <div class="nodisplay">
        Imgur is home to the web's most popular image content, curated in real time by a dedicated community through commenting, voting and sharing.
    </div>

    <div id="hallway">
        <div class="container">
            <div id="cat1" class="painting">
                <img src="//s.imgur.com/images/404/cat1weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="cat2" class="painting">
                <img src="//s.imgur.com/images/404/cat2weyes.png">
                <div class="eye-container">
                    <div class="eye">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="giraffe" class="painting">
                <img src="//s.imgur.com/images/404/giraffeweyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
                <img class="monocle" src="//s.imgur.com/images/404/monocle.png" />
            </div>
            <div id="cat3" class="painting">
                <img src="//s.imgur.com/images/404/cat3weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
            <div id="cat4" class="painting">
                <img src="//s.imgur.com/images/404/cat4weyes.png">
                <div class="eye-container">
                    <div class="eye left">
                        <div class="pupil"></div>
                    </div>
                    <div class="eye right">
                        <div class="pupil"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="footer textbox">
        <h1>Zoinks! You've taken a wrong turn.</h1>
        <p>Let's split up, gang. If you're looking for an image, it's probably been deleted or may not have existed at all.</p>
        <p>If you are looking for groovy images, <a href="//imgur.com">visit our gallery!</a></p>
        <a href="//imgur.com" class="footer-logo"><img src="https://s.imgur.com/images/imgurlogo-header.png"></a>
    </div>

    <script type="text/javascript">
        (function(widgetFactory) {
            widgetFactory.mergeConfig('analytics', {
                isAdmin: false
            });
        })(_widgetFactory);
    </script>

    <script type="text/javascript" src="https://s.imgur.com/min/404.js?1393899213"></script>
    <script type="text/javascript">
            var e404 = E404.getInstance();
            e404.generalInit();
    </script>
</body>
</html>

I have no experience with calling a REST API. I searched for Delphi demo information, but I did not find much. I need some guidance about this.

Delphi 10.4 / Windows 10

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mitchell Hu
  • 105
  • 1
  • 6
  • 3
    According to [the documentation](https://apidocs.imgur.com/#c85c9dfc-7487-4de2-9ecd-66f727cf3139) the resource for uploading images is `request.Resource := '3/upload';`. You should also use OAuth 2.0 to authenticate yourself via `Authorization: Bearer {YOUR_ACCESS_TOKEN}` HTTP header. Imgur [allows you](https://apidocs.imgur.com/#authorization-and-oauth) to upload images anonymously via `Authorization: ClientID {YOUR_CLIENT_ID}`. Anonymously uploaded images will not be tied to your account. – Peter Wolf Jan 04 '23 at 13:37
  • @PeterWolf that should be posted as an answer instead of a comment – Remy Lebeau Jan 04 '23 at 18:18
  • 1
    You literally request https://api.imgur.com/a/C11W7xC which doesn't exist, and just because you expected `application/json` doesn't mean you also get that. What you question output does not contain (despite your code) is `response.StatusText`, which is most likely `Not Found`. If you would check [`response.StatusCode`](https://docwiki.embarcadero.com/Libraries/Sydney/en/REST.Client.TCustomRESTResponse.StatusCode) it would tell you [`404` instead of `200`](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). Requires a minimum of understanding HTTP. – AmigoJack Jan 04 '23 at 19:19
  • @RemyLebeau thanks for your help to revise my question Title. and your suggestion is right, Peter Wolf comment is the final answer. Peter could you add an Answer, allow me to mark it as the right one. – Mitchell Hu Jan 05 '23 at 03:19
  • @AmigoJack you are right, about the Status.Text is "Failed Not Found:". Finally, I modify the code as Perter Wolf noticed and it works fine now. StatusCode = 200, So, I can parse the Response.contet JSON String and get the Picture link. thanks. – Mitchell Hu Jan 05 '23 at 05:39

1 Answers1

2

You are trying to upload an image to an invalid resource a/C11W7xC on the server, which is why you get an HTTP 404 Not Found response with HTML content.

According to the documentation, the resource for uploading images is 3/upload instead.

I haven't used the API myself, but it seems to me that the authorization you are using is not in line with Imgur's authorization.

Imgur's API allows you to upload images either anonymously via an Authorization: ClientID {YOUR_CLIENT_ID} HTTP header, or using an Authorization: Bearer {YOUR_ACCESS_TOKEN} HTTP header to tie the uploaded image to your account. See Authorization and OAuth on how to obtain the access token.

Note that you should not share your client credentials with the whole world, it has secret in its name, afterall. I recommend you should renew your client credentials at this point.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Peter Wolf
  • 3,700
  • 1
  • 15
  • 30