Home > Blockchain >  Uploading pic to imgur by TRESTClient is not working
Uploading pic to imgur by TRESTClient is not working

Time:01-06

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 >
        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 >
            <div id="cat1" >
                <img src="//s.imgur.com/images/404/cat1weyes.png">
                <div >
                    <div >
                        <div ></div>
                    </div>
                    <div >
                        <div ></div>
                    </div>
                </div>
            </div>
            <div id="cat2" >
                <img src="//s.imgur.com/images/404/cat2weyes.png">
                <div >
                    <div >
                        <div ></div>
                    </div>
                </div>
            </div>
            <div id="giraffe" >
                <img src="//s.imgur.com/images/404/giraffeweyes.png">
                <div >
                    <div >
                        <div ></div>
                    </div>
                    <div >
                        <div ></div>
                    </div>
                </div>
                <img  src="//s.imgur.com/images/404/monocle.png" />
            </div>
            <div id="cat3" >
                <img src="//s.imgur.com/images/404/cat3weyes.png">
                <div >
                    <div >
                        <div ></div>
                    </div>
                    <div >
                        <div ></div>
                    </div>
                </div>
            </div>
            <div id="cat4" >
                <img src="//s.imgur.com/images/404/cat4weyes.png">
                <div >
                    <div >
                        <div ></div>
                    </div>
                    <div >
                        <div ></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div >
        <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" ><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

CodePudding user response:

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.

  • Related