r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
985 stars 1.99k forks source link

Special Characters in GET requests #663

Closed Alec-Stashevsky closed 4 years ago

Alec-Stashevsky commented 4 years ago

I am translating a cURL command into a httr::GET request. When I test the command in a git terminal using regular cURL syntax I am able to get a successful query. However, there is some issue going on with the httr::GET query syntax. I believe it may be due to the brackets or that only a username is required to gain access to the API.

I am omitting the API key for security.

For reference here is the original curl command:

$ curl https://api.goclimate.com/v1/flight_footprint \
  -u YOUR_API_KEY: \
  -d 'segments[0][origin]=ARN' \
  -d 'segments[0][destination]=BCN' \
  -d 'segments[1][origin]=BCN' \
  -d 'segments[1][destination]=ARN' \
  -d 'cabin_class=economy' \
  -d 'currencies[]=SEK' \
  -d 'currencies[]=USD' \
  -G

And how I have translated that to R which is generating a 400 error and returning a json content as "invalid_request_error"

r <- GET("https://api.goclimate.com/v1/flight_footprint",
    authenticate(api.key,""),
    query = list(
        "segments[0][origin]"="ARN",
        "segments[0][destination]"="BCN",
        "segments[1][origin]"="BCN",
        "segments[1][destination]"="ARN",
        "cabin_class"="ecomony",
        "currencies[]"="SEK",
        "currencies[]"="USD"))
jennybc commented 4 years ago

I haven't read your issue closely but just want to point out a tool that is generally helpful:

Tools to Transform ‘cURL’ Command-Line Calls to ‘httr’ Requests https://cinc.rud.is/web/packages/curlconverter/index.html