API mercadolivre does not return JSON

Asked

Viewed 1,159 times

0

When I glue the address https://api.mercadolibre.com/sites/MLB/categories in the browser I see that the restfull API of the free market returns the data in JSON format, but when accessing the same link using the Restdebuger that comes with Delphi (Tools/Rest Debuger) I can only receive text/html.

I also tried to put a Restclient component in Delphi xe7 and configure the urlbase but gave it anyway. Someone knows how to do?

Note that this boy managed to do in delphiXE6, but this example did not work with me. See the link http://edgartec.com/delphi-xe6-consumir-la-api-rest-de-mercado-libre/

object RESTClient1: TRESTClient
  Accept = 'application/json, text/plain; q=0.9;q=0.8,'
  AcceptCharset = 'UTF-8, *;q=0.8'
  AcceptEncoding = 'identity'
  BaseURL = 'https://api.mercadolibre.com/sites/MLB'
  ContentType = 'application/x-www-form-urlencoded'
  Params = <>
  HandleRedirects = True
  Left = 168
  Top = 8
end
object RESTRequest1: TRESTRequest
  Accept = 'application/json, text/plain; q=0.9;q=0.8,'
  Client = RESTClient1
  Params = <
    item
      name = 'q'
      Value = 'Smartphone'
    end
    item
      name = 'price'
      Value = '700.0-1000.0'
    end>
  Response = RESTResponse1
  SynchronizedEvents = False
  Left = 240
  Top = 8
end
object RESTResponse1: TRESTResponse
  ContentType = 'application/json'
  RootElement = 'categories'
  Left = 336
  Top = 8
end

2 answers

2

This URL:

https://api.mercadolibre.com/sites/MLB/categories

Forwards to API documentation, not to JSON code. However, according to this same documentation. The URL is correct.

I ran a test using Chrome’s Postman extension, but specified the type of content in the request:

Content-Type: application/json

And in this test, the API returned validated JSON code.

inserir a descrição da imagem aqui

I couldn’t fix your code because you didn’t even provide it. However, your problem is that you’re not specifying the type of content you’re ordering. Most Apis don’t care about that. But this binds. If the component you are using does not support a change in the request header, you may not be able to use it in this API.

  • Thanks for your reply. I did not put code because I have no code. As I said, I used the Restdebugger tool, which generated the following objects: (see in the edited question)

1

I just put "application/json" on RESTClient1.accept and it worked.

  • 1

    That, was the definition of the type the problem.

Browser other questions tagged

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