Access Free Market API by Delphi XE5

Asked

Viewed 1,900 times

0

I’m trying to make an application in Delphi to integrate ERP to the Free Market, and when using REST Debugger (for testing) this returning me an HTML and not Json, someone has already gone through it?

If using Postman returns the correct Json, however Delphi or REST Debugger only returns HTML.

Code used:

RESTClient.Accept        := 'application/json'; 
RESTClient.BaseURL       := 'api.mercadolibre.com/currencies?id=BRL';
RESTClient.AcceptCharset := 'UTF-8'; 
RESTClient.ContentType   := 'application/json';
RESTRequest.Method       := TRESTRequestMethod.rmGET;   
RESTRequest.Execute; 

Should return

{ "id": "BRL", "symbol": "R$", "description": "Real", "decimal_places": 2} 

Returns

<!DOCTYPE html><!--if lt IE 7 ]> <span class="nt">"id"</span><span class="p">:</span> <span class="s2">"BRL"</span>
  • So, you’re passing Content-Type ?

  • Modify your question by adding if possible at least part of the call code of your Delphi Rest function.

  • Good morning, The code used is as follows:

  • Good morning, The code used is seginte: Restclient.Accept := 'application/json'; Restclient.Baseurl := 'https://api.mercadolibre.com/currencies?id=BRL'; Restclient.Acceptcharset := 'UTF-8'; Restclient.Contenttype := 'application/json'; Restrequest.Method := Trestrequestmethod.rmGET; Restrequest.Execute; Should return { "id": "BRL", "Symbol": "R$", "Description": "Real", "decimal_places": 2} Returns <! DOCTYPE html><! --if lt IE 7 ]> <span class="nt">"id"</span><span class="p">:</span> <span class="s2">"BRL"</span>... I use Delphi XE5. Grateful.

  • @WANDARLEIMICHELON edits the question, don’t put your code in the comments, because we can’t help you like this ;/

2 answers

2

The best option when the REST API doesn’t help is to use the old Indy. First we copy the initial part where the json should come, then just clean the string

var
  vTemp : String;
begin
  vTemp := IdHTTP1.GET('https://api.mercadolibre.com/currencies/BRL');

  vTemp := Copy(vTemp, Pos('{', vTemp) + 1, Length(vTemp));
  vTemp := Copy(vTemp, 1, Pos('}', vTemp) - 1);

  vTemp := StringReplace(vTemp, '<span class="collapsible">', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '<span class="nt">', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '<span class="mi">', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '<span class="p">', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '<span class="s2">', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '</span>', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, #$A, '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, '"', '', [rfReplaceAll]);
  vTemp := StringReplace(vTemp, ' ', '', [rfReplaceAll]);
  ShowMessage(vTemp);

I already use this same replacement structure for another part of the same Api.

Note: The API it is using does not return a JSON (Directly to Delphi), it returns a result for display in the browser. Functional I would show if the API offered support for it. It once offered, today no more! documentation Official she offers some Sdks for other platforms.

  • I believe that the ideal here would be to show a functional way of using REST, although the alternative is valid.

  • 1

    The API it is using does not return a JSON, it returns a result to display in the browser. Functional I would show if the API offered support for such. Formerly it offered, today no more!

  • Cool, Junior! Can you add that information to the answer, please? Then I award the reward! :)

1

What was missing:

RESTRequest.Accept        := 'application/json'; 

Same for when you use the Indy:

xIdHttp.Request.Accept := 'application/json';
  • But in the example code he posted, you already have this line

Browser other questions tagged

You are not signed in. Login or sign up in order to post.