How to pass parameter to REST API?

Hi there, one question on how to pass parameter to the REST API REQUEST.

I already got the token and I want to am using R, to retrieve data from REST API, by passing the parameters. the following is my code


# once get the token, retrieve the data

library(httr) 
r <- POST("https://XXXXXXXX/api/locationhazardInfo",
           add_headers("Content-Type"="text/plain; charset=UTF-8",
                       Accept="text/plain",
                       "Authorization"=paste("Bearer", tok)),
           body = list(
             "Latitude":40.738269,
             "Longitude":-74.02826,
             "CountryCode":"USA",
             "HazardLayers":[  
               {  
                 "LayerId":"18",
                 "Description":""
               },
               {  
                 "LayerId":"6",
                 "Description":""
               }
             ],
             "Distances":[  
               {  
                 "Value":1,
                 "Unit":"miles"
               }
             ]
        )
)

The tok is the token I got from previous step. And I got the systen errors (seem all syntax erros) as shown below,

Can someone give me some idea on what might cause the issue and the resolution? Any input is greatly appreciated. Thanks.

You can use query. e.g.:

data <- httr::GET(url,
                    httr::add_headers("Content-Type" = "application/json",
                                      "Accept" = "application/json",
                                      "apikey" = Sys.getenv("api_key")),
                    query = list("year" = 2008)
  )

For the errors, post your code instead of the screenshot.

Hi Williaml, thanks for your reply and solution. I copied and pasted my code in my previous post, but I pasted here again.


library(httr)
r <- POST("[https://XXXXXXXX/api/locationhazardInfo](https://xxxxxxxx/api/locationhazardInfo)",
add_headers("Content-Type"="text/plain; charset=UTF-8",
Accept="text/plain",
"Authorization"=paste("Bearer", tok)),
body = list(
"Latitude":40.738269,
"Longitude":-74.02826,
"CountryCode":"USA",
"HazardLayers":[
{
"LayerId":"18",
"Description":""
},
{
"LayerId":"6",
"Description":""
}
],
"Distances":[
{
"Value":1,
"Unit":"miles"
}
]
)
)
1 Like

Possibly like this? Use = instead of :.

I don't have the API details, but it gets rid of the errors:

library(httr)
r <- POST("https://XXXXXXXX/api/locationhazardInfo",
          add_headers("Content-Type"="text/plain; charset=UTF-8",
                      Accept="text/plain",
                      "Authorization"=paste("Bearer", tok)),
          body = list(
            "Latitude"=40.738269,
            "Longitude"=-74.02826,
            "CountryCode"="USA",
            "HazardLayers"='[
              {
                "LayerId"="18",
                "Description"=""
              },
              {
                "LayerId"="6",
                "Description"=""
              }
              ]',
            "Distances"='[
              {
                "Value"=1,
                "Unit"="miles"
              }
              ]'
          )
)

Otherwise something like this?

"HazardLayers" = list("LayerId":"18", "Description":"6")

Hi Williaml, thank you very much for your help. I found the solution here:

rg <- POST(url, 
           # add_headers("Content-Type"="text/plain; charset=UTF-8",
           add_headers("Content-Type"="application/json",            
                       Accept="text/plain",
                       "Authorization"=paste("Bearer", tok)),
           body = '{
         "Latitude":40.738269,
         "Longitude":-74.02826,
         "CountryCode":"USA",
         "HazardLayers":[
           {
              "LayerId":"18",
              "Description":""
            },
           {
              "LayerId":"6",
              "Description":""
           }
         ],
         "Distances":[
           {
              "Value":1,
              "Unit":"miles"
           }
         ]
       }' ,
           encode = "raw"  #### , verbose()
)

Thanks again for your help.

1 Like

3 posts were merged into an existing topic: Error resource owner password credential in R API

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.