IPFS RPC API : ipfs routing findprovs provides an unreadable response

46 Views Asked by At

I have a fresh install of IPFS daemon on my computer :

ipfs init
ipfs daemon

When running the following command, the output seems correct:

$ ipfs routing findprovs QmUZzfuaCcPSXgADUnkF1GKBsDRUxxxxxxxxx
12D3KooWLJBTtGHSbeqHSEkAo4jTni1PBrtRktV2a85yKxxxxxx

When requesting the RPC API as follows (should give the same input as CLI):

curl -X POST "http://127.0.0.1:5001/api/v0/routing/findprovs?arg=QmUZzfuaCcPSXgADUnkF1GKBsDRUxxxxxxxxx"

The response I get contains multiple JSON objects (100+) separated by \n

{"Extra":"","ID":"12D3KooWAH9RHKrbpS1nZC7Wos91y8dD4HwN5Wq2JmHjxxxxxxx","Responses":null,"Type":7}
{"Extra":"","ID":"12D3KooWHWEKWG6Mh4ZgoCmxhP2uhyyVdBJGt388LMLXQQxxxxxx","Responses":null,"Type":0}
....

How can I get the same result as the CLI using the RPC API ?

1

There are 1 best solutions below

0
On

I found a temporary solution (that is really not ideal) after further investigation and confirmation from this github post

Turns out the returned providers with Type=4 are the ones identified by the CLI. Here is a ruby example to extract those from the response (after treating the response as plain text and not Json) :

response.body.each_line
             .map{     |line|           JSON.parse(line) }
             .filter{  |parsed_line|    parsed_line['Type'] == 4 }
             .collect{ |parsed_line|    parsed_line['Responses'] }
             .map{     |responses|      responses[0] }
             .map{     |first_response| first_response['ID'] }

# ["QmQzqxhK82kAmKvARFZSkUVS6fo9sySaiogAnx5Enxxxx",
# "12D3KooWFzZYEChh6Dsy4jHyTopK42oRrk41WVfnpMG5rxxxxxx",