0

I am new to this topic and reviewed several SO answers before, but still cannot figure it out.

Trying to access API, using R:

curl -X POST "http://api.spending.gov.ua/api/rest/1.0/transactions" -H "accept: application/json" -H 
"Content-Type: application/json" -d "{ \"payers_edrpous\": [ \"string\" ], \"recipt_edrpous\": [ 
\"string\" ], \"startdate\": \"string\", \"enddate\": \"string\", \"regions\": [ 0 ]}"

My current stage

library(httr)
r <- GET("http://api.spending.gov.ua/api/rest/1.0/transactions")

status_code(r)

This works, I have 200 response. But how to write a query to get data in json format? Appreciate any tips.

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • 2
    Have you seen this post: https://stackoverflow.com/questions/56958500/how-to-make-a-post-request-with-header-and-data-options-in-r-using-httrpost ? This might point you in the right direction. – dcruvolo May 11 '20 at 11:45
  • 1
    Do you have some sample data that can be tested on the API test page? – Dave2e May 12 '20 at 19:33
  • @dcruvolo, I checked it, still do not understand :(. – Anakin Skywalker May 12 '20 at 20:30
  • @Dave2e, apologize, not sure what do you mean :(. If you can tell me any example of how to query any data from there, using their variables, I would be very grateful. – Anakin Skywalker May 12 '20 at 20:31
  • 1
    I don't understand the language thus it makes it impossible to fill in the blanks. So values for payers_edrpous, recipt_edrpous, start date, endnote and regions would help – Dave2e May 12 '20 at 20:44
  • @Dave2e, oh, I really apologize for this! payers_edrpous means an 8-digit code, a real one is 00013534, start date for example as "31.03.2020", region is probably "Центральне", is probably the code of the recipient, end_note - not sure here, maybe a transaction description. If you can show me how to connect and search by payers_edrpous and start date, would be perfect, I will figure it out myself then. Appreciate your time! – Anakin Skywalker May 12 '20 at 21:26

1 Answers1

2

The link from @dcruvolo was helpful.

In order get to this to work, you need to start with some valid values. From the API link in the question, there is a test page to order to test the submittal:

enter image description here

One can substitute in test values and then press the "Execute" button submit values. Attempted values from the comments above, valid enough not to cause an error but also did not return any valid results.

To perform the POST in R here is an example:

posting<-'{
  "payers_edrpous": [
    "00013534"
  ],
  "recipt_edrpous": [
    ""
  ],
  "startdate": "2020-03-01",
  "enddate": "2020-03-28",
  "regions": [
    0
  ]
}'

library(httr)
r <- POST("http://api.spending.gov.ua/api/rest/1.0/transactions", body=posting, 
          httr::add_headers(`accept` = 'application/json'), 
          httr::content_type('application/json')) #encode="json"
content(r)

Posting is the JSON body to pass, edit this as necessary. All variables are strings except "regions" where it is an integer, not sure what the valid range is.

Sorry this is the best I can do. Good luck.

Dave2e
  • 22,192
  • 18
  • 42
  • 50