Access Delphi XE5 Free Market APIs

0

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

If you use Postman it returns the correct Json, however by Delphi or REST Debugger it 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>
    
asked by anonymous 11.10.2017 / 19:35

1 answer

2

The best option when the REST API does not help is to use the old Indy. First we copy the initial part where the json should come, then we just have to clear 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 that 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. Formerly she offered, not today anymore! At official documentation she offers some Sdks to other platforms.

    
28.11.2017 / 17:25