No HTTP resource was found when use REST API in R

Options
Yarch
Yarch Newcomer

Hi all,

I'm trying to use REST API to connect TRTH in R, now I'm having problem with my code

library(httr)
uri1 <- "https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch"
token1 <- paste("Token", cred[2], sep = " ")

body1 <- '{ "Request": { "Identifier": "TRI.N", "IdentifierType": "Ric", "Range": { "Start": "2015-11-17T00:00:00.000Z", "End": "2015-11-24T00:00:00.000Z" } } }'

a <- httr::GET(uri1, body = body1, add_headers(Prefer = "respond-async",Authorization = token1))
result <- httr::content(a, "parsed", "application/json")

In the above code I save my token in cred[2]

After I run my code, I get this result

$error
$error$message [1]
"No HTTP resource was found that matches the request URI 'https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch'."

Looks like I have a wrong URI input, but I just copy this address from sample code.

I appreciate that if anybody can help me with this issue.

Best Answer

  • Yarch
    Yarch Newcomer
    Answer ✓

    @chavalit-jintamalit

    Now I solve this problem, I really appreciate your help

    For the convenience for all other guys, here is the correct code .

    The mistake I have is httr::POST part

    I should declare content_type_json() in the header first

    getIdentifier <- function(cred, Identifier, IdentifierType){
    uri1 <- "https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch"

    part1 <- c('{"Request": {"Identifier": "')
    part2 <- c('","IdentifierType": "')
    part3 <- c('", "Range": {
    "Start": "2015-11-17T00:00:00.000Z",
    "End": "2015-11-24T00:00:00.000Z"}}}')
    token1 <- paste("Token", cred[2], sep = " ")
    request <- paste(part1, Identifier, part2, IdentifierType, part3, sep = "")
    a <- httr::POST(uri1, body = request,
    add_headers(Prefer = "respond-async",Authorization = token1),
    content_type_json())
    result <- httr::content(a, "parsed", "application/json")
    return(result)
    }

Answers