question

Upvotes
Accepted
15 1 3 5

No HTTP resource was found when use REST API in R

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.

tick-history-rest-apirest-apir
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
Accepted
15 1 3 5

@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)
}
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
18.2k 21 13 21

@Yarch , please refer to the developer guide here

On page number 39th, the uri is correct but it is a HTTP POST method.

But your code seems to use a GET method (httr::GET) ? Please confirm.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
15 1 3 5

@chavalit.jintamalit

Thank you for point this out, actually I tried both method. If I use httr:POST I will obtain a different error message

$error $error$message [1] "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."

I appreciate your apply, hope to see your feedback soon

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
18.2k 21 13 21

@Yarch

This error means that you have not defined Content-Type in your http request header.

Please follow the sample request from the developer guide.

The Content-Type is "application/json"


request.png (56.8 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
15 1 3 5

@chavalit.jintamalit

Here is the detailed information I observed, I hope it could be helpful regarding on this issue

Starting from variable "a", my output is incorrect

Here is the output of a

Response [https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch] 
Date: 2017-09-27 00:20
Status: 404
Content-Type: application/json; charset=utf-8
Size: 157 B

Based on this description, I think the issue is related to the uri i'm using

And for the The Content-Type you just mention in previous reply, I think I set up the correct format.

For your information, here is another example I write in R, which works fine

getUserInfo <- function(cred){
part1 <- "https://hosted.datascopeapi.reuters.com/RestApi/v1/Users/Users("
part2 <- ")"
uri1 <- paste(part1, cred[1], part2, sep = "")
token1 <- paste("Token", cred[2], sep = " ")
a <- httr::GET(uri1, add_headers(Prefer = "respond-async", Authorization = token1))
result <- httr::content(a, "parsed", "application/json") 
return(result)
}
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Please refer to page 38th on the developer guide.

From the sample Request, this API call does not require Content-Type nor body.

So it is working as expected.

user.png (106.5 KiB)

Yes you are right. Thanks for this clarification

Upvote
18.2k 21 13 21

@Yarch

I just did 3 tests on the API calls:

1. POST method following the sample request from developer guide

Request:

POST https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch HTTP/1.1
Prefer: respond-async
Content-Type: application/json
Authorization: Token <mytoken>
Cache-Control: no-cache

{
  "Request": {
    "Identifier": "US4592001014",
    "IdentifierType": "Isin",
    "Range": {
      "Start": "2008-01-01T00:00:00.000Z",
      "End": "2008-01-01T00:00:00.000Z"
    }
  }
}

Response:

HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8

{"@odata.context":"https://hosted.datascopeapi.reuters.com/RestApi/v1/$metadata#Collection(ThomsonReuters.Dss.Api.Search.HistoricalSearchResult)","value":[{"Identifier":"IBM","IdentifierType":"Ric","Source":"","Key":"VjF8MHgzMDAwMDAwMDAwMDAwMDAwfDB4MzAwMDAwMDAwMDAwMDAwMHx8fHx8fHxJQk18","Description":"Historical Instrument","InstrumentType":"Unknown","Status":"Valid","DomainCode":"6","FirstDate":"1996-01-02T00:00:00.000Z","LastDate":"2017-09-26T00:00:00.000Z","History":[]},...<a lot of entries here>...}

2. POST method with incorrect Content-Type header

Request:

POST https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch HTTP/1.1
cache-control: no-cache
Prefer: respond-async
Content-Type: application/octet-stream
Authorization: Token <mytoken>

{
  "Request": {
    "Identifier": "US4592001014",
    "IdentifierType": "Isin",
    "Range": {
      "Start": "2008-01-01T00:00:00.000Z",
      "End": "2008-01-01T00:00:00.000Z"
    }
  }
}

Response:

HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Content-Length: 118
Content-Type: application/json; charset=utf-8

{"error":{"message":"The request entity's media type 'application/octet-stream' is not supported for this resource."}}

3. GET method

Request:

GET https://hosted.datascopeapi.reuters.com/RestApi/v1/Search/HistoricalSearch HTTP/1.1
cache-control: no-cache
Prefer: respond-async
Content-Type: application/json
Authorization: Token <mytoken>

Please note that HTTP GET method cannot contain body.

Response:

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8

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

I do not put all the detail headers here, just only related headers to troubleshoot the issue.

Hope this is more clear now.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@chavalit.jintamalit

Your reply is the key issue that help me solve this problem.

Since this issue is related to R, I post the correct solution for this issue and accept it as highlight answer.

If you want this reply to be highlight, I will cancel my accept and highlight this long reply from you

Upvotes
15 1 3 5

@chavalit.jintamalit

Thanks for your clarification. It makes sense. And I notice that the 3rd example you provide has the identical error that I have

So maybe I need to declare this Content type at the beginning of my code.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.