httr::POST use api key

I'm trying to figure out how to do the following with httr::POST

curl -X POST -u "apikey:{apikey}" --header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Hello\"}}" "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/{workspace_id}/message?version=2018-09-20"

In the past I have used httr::authenticate(username,password) like follows:

conv.init <- httr::POST(url=paste(url,"/workspaces/",workspace,"/message",version,sep=""),
                        httr::authenticate(username,password),
                        httr::add_headers("Content-Type"="application/json"),
                        body = '{ "input": { "text":""},
                           "system":{ "dialog_stack":["root"]},
                           "dialog_turn_counter":1,
                           "dialog_request_counter":1}',
                        encode = "json")

How can I pass an api key instead of a user name and password?

Thanks!

1 Like

I figured out. This works:

httr::POST(url=glue::glue("{url}/{workspace}/message?version={version}"),
                        httr::authenticate("apikey",api_key),
                        httr::add_headers(c("Content-Type"="application/json")),

                        body = '{ "input": { "text":""},
                        "system":{ "dialog_stack":["root"]},
                        "dialog_turn_counter":1,
                        "dialog_request_counter":1}',
                        encode = "json") 
4 Likes

This topic was automatically closed 7 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.