Array in body for httr POST request - r

This curl call works to create a new droplet on Digital Ocean
curl -X POST "https://api.digitalocean.com/v2/droplets" \
-d '{"name":"test3","region":"nyc2","size":"512mb","image":5562742,"ssh_keys":[89103]}' \
-H "Authorization: Bearer $TOKEN"
-H "Content-Type: application/json"
However, I'm having trouble getting an httr::POST() request to work only when the parameter ssh_keys is given. In the above method the ssh_keys parameter, if given, has to be an array.
I assumed the list of parameters could be passed to the body as, e.g., where the ssh_keys parameter is inside a list
args <- list(name="test3", region="nyc2", size="512mb", image="5562742", ssh_keys=list(891111))
POST(url, config=auth, body=args)
I assume this is what's happening on the inside:
jsonlite::toJSON(args)
[1] "{ \"name\" : [ \"test3\" ], \"region\" : [ \"nyc2\" ], \"size\" : [ \"512mb\" ], \"image\" : [ \"5562742\" ], \"ssh_keys\" : [ [ 89103 ] ] }"
Which I imagine would work, but perhaps that's not what's happening? Fiddling with the encode parameter in POST doesn't seem to help.
The curl call works from terminal, but using httr::POST() I keep getting the error message
You specified invalid ssh key ids for Droplet creation.

In this specific case,
x <- jsonlite::toJSON(args, auto_unbox=TRUE)
cat(x)
seems to return the correct format (assuming the problem is not with the headers) so them
POST(url, config=auth, body=x)
should send the correct request.

Maybe something like:
req <- POST(
url = "https://api.digitalocean.com/v2/droplets",
body = toJSON(args, auto_unbox=TRUE),
add_headers (
"Content-Type" = "application/json",
"Authorization" = paste("Bearker", TOKEN)
)
)

Related

Creating an Item in a Board using an Http request/cURL

Hi I have this cURL request that I have been trying and failing to get it right, it gives me a 500 Error (Internal Error)
Please see my curl request below:
curl --location --request POST "https://api.monday.com/v2" --header "Authorization: XXXXX" --header "Content-Type: application/json" --data-raw "{\"query\":\"mutation { create_item (board_id: 1622487816,group_id: \"emailed_items\", item_name: \"Test from Curl\") { id } }\"}" -v
I get back an empty object as a response but on the response header I see a 500 error message
Make sure your quotes are nested and escaped properly.
Your code:
"{\"query\":\"mutation { create_item (board_id: 1622487816,group_id: \"emailed_items\", item_name: \"Test from Curl\") { id } }\"}"
You are right to begin with a double quote and you are right to escape the first set of quotes within those quotes.
However, when you get further in the query, "emailed_items" also needs to be escaped, but since you are already in a set of quotes, you actually need three backslashes.
Corrected code:
"{\"query\":\"mutation { create_item (board_id: 1622487816,group_id: \\\"emailed_items\\\", item_name: \\\"Test from Curl\\\") { id } }\"}"
Make use of the npm package for monday.com queries. One such package is monday-sdk-js
Assigning parameters could be confusing thus make use of this package which takes query as a parameter and makes things easy.

Translate curl call using httr in R, with multiple calls

I am trying to "translate" the curl call below, so that I can make an API call with R (using httr), with no luck. I have tried curlconverter and using the suggestion here. However, the API I want to access has multiple layers, and parenthesis are all over the place, which complicate conversion. Any suggestions for a function that will translate this recurring logic dynamically?
curl call:
curl -X POST 'https://api.notion.com/v1/databases/897e5a76ae524b489fdfe71f5945d1af' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Notion-Version: 2021-05-13' \
-H "Content-Type: application/json" \
--data '{
"filter": {
"or": [
{
"property": "In stock",
"checkbox": {
"equals": true
}
},
{
"property": "Cost of next trip",
"number": {
"greater_than_or_equal_to": 2
}
}
]
},
"sorts": [
{
"property": "Last ordered",
"direction": "ascending"
}
]
}'
Desired outcome (function)
api_call(page, token, filters)
This question is a bit difficult to answer, since you have the access key and therefore nobody can test code to make sure it works. However, in terms of simply translating the curl call to httr code, I think the following code will do so.
library(httr)
library(jsonlite)
# Create the body of the POST request
json_body <- list(
"filter" = list(
"or" = list(
list(
"property" = "In stock",
"checkbox" = list(
"equals" = "true"
)
),
list(
"property" = "Cost of next trip",
"number" = list(
"greater_than_or_equal_to" = 2
)
)
)
),
"sorts" = list(
list(
"property" = "Last ordered",
"direction" = "ascending"
)
)
)
# Make post request
request <- POST(
url = "https://api.notion.com/v1/databases/897e5a76ae524b489fdfe71f5945d1af",
add_headers("Authorization" = paste("Bearer", notion_api_key),
"Notion-Version" = "2021-05-13"),
body = json_body,
encode = "json"
)
In terms of defining a function that creates the body dynamically, that's simply a question of formatting the filters similarly to the above example.

Writing a proper API request in R for curl -X POST

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.
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:
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.

How to use Optional Headers in Artifactory REST API?

I'm able to search using
GET /api/search/prop?[p1=v1,v2][&p2=v3][&repos=x[,y]]
as per the documentation. But how to use optional headers to get extra information for the found artifacts?
You need to set the HTTP header X-Result-Detail to properties. Using a curl command it would look something like this
curl -uUSERNAME:PASSWORD --request GET \
--url 'http://jfrog.local/artifactory/api/search/prop?build.number=1&repos=generic-prod' \
--header 'X-Result-Detail: properties'
This will look in my repository called generic-prod for all artifacts that have build.number = 1 as a property.
That would return with the results you're looking for
{
"results" : [ {
"properties" : {
"build.name" : [ "docker-jenkins-build" ],
"build.number" : [ "1" ],
"build.timestamp" : [ "1556557591780" ]
},
"uri" : "http://jfrog.local:80/artifactory/api/storage/generic-prod/jenkins.zip"
} ]
}
This lists the properties I have for my artifact.

Connecting to BI tool's REST API using rcurl

I am trying to connect directly to a BI tool's API from within R. The API Documentation lists the curl command below to obtain an authentication token:
curl -X POST -H "Content-Type: application/json" -d
'{
"email": "your#email.com",
"password": "your_password"
}'
https://app.datorama.com/services/auth/authenticate
Further, below is an example of a JSON query that can be used to query data:
{
"brandId": "9999",
"dateRange": "CUSTOM",
"startDate": "2016-01-01",
"endDate": "2016-12-31",
"measurements": [
{
"name": "Impressions"
}
],
"dimensions": [
"Month"
],
"groupDimensionFilters": [],
"stringDimensionFilters": [],
"stringDimensionFiltersOperator": "AND",
"numberDimensionFiltersOperator": "AND",
"numberMeasurementFilter": [],
"sortBy": "Month",
"sortOrder": "DESC",
"topResults": "50",
"groupOthers": true,
"topPerDimension": true,
"totalDimensions": []
}
I'm trying to 1) translate the curl command above into R so as to obtain the authentication token required, and 2) query the data through the JSON script above.
So far i've attempted using the httr library as follows:
library(httr)
r <- POST('https://app.datorama.com/services/auth/authenticate',
body = list(
brandId = "9999",
dateRange = "CUSTOM",
measurements = list(name="Impressions"),
dimensions = list(name="Month"),
startDate = "2016-01-01",
endDate = "2016-12-31"
),
encode = "json",
authenticate("username", "password"))
to no avail.
The API documentation is behind a password protected page and as such i'm unable to link it. Let me know if additional information is required.
hrbrmstr is totally right! you should generate two api calls, the first one is to authenticate the user and the second one is to query the data.
Below is a fully working example of using the Datorama query API from R. feel free to contact the Datorama support for any additional questions.
library(httr)
res <- POST("https://app.datorama.com/services/auth/authenticate",
body=list(email="your#email.com",
password="your_password"),
encode="json")
token <- content(res)$token
res_query <- POST(paste("https://app.datorama.com/services/query/execQuery?token=",token, sep=""),
body = list(
brandId = "9999",
dateRange = "CUSTOM",
measurements = list(list(name = "Impressions")),
dimensions = list("Month"),
startDate = "2016-01-01",
endDate = "2016-12-31"
),
encode = "json")
cat(content(res_query, "text"), "\n")
I don't have access to their API and would have prbly written a small wrapper pkg to this service if they had a free tier. Having said that,
curl -X POST \
-H "Content-Type: application/json" \
-d '{ "email": "your#email.com",
"password": "your_password" }'
translates to:
library(httr)
res <- POST("https://app.datorama.com/services/auth/authenticate",
body=list(email="your#email.com",
password="your_password"),
encode="json")
They don't have their app API documentation online for free either but I'm going to assume it sends back a JSON response with some type of authorization_token and encoded string.
You then—most likely—need to pass that result with every subsequent API call (and there is probably a timeout where the initial auth will need to be re-upped).
authenticate() is used for HTTP basic auth, not for this type of in-API JSON/REST auth.
Your actual API call looks fine apart from using the token auth.

Resources