InseeFrLab / InseeFrLab/melodi

main url is text/html but package is trying to read json

Open
#4 1 comment 0 reactions 1 assignee Claimed by @cbobinec View on GitHub
Dominant language
R
Stars
9
Forks
2
Avg merge
8m
Merged PRs (30d)
5

Description

Hi,

I am having issues using the package right from the beginning.

When I do the simplest test, I get an error :

> ```R
> # testing melodi's get_catalog method
> > melodi::get_catalog()
>
> Request all catalog : https://api.insee.fr/melodi/catalog/all
> Error in `httr2::resp_body_json()`:
> ! Unexpected content type "text/html".
> • Expecting type "application/json" or suffix "json".
> ```

so I backtrace it, and get :
> ```R
> > rlang::last_trace()
>
>
> Error in `httr2::resp_body_json()`:
> ! Unexpected content type "text/html".
> • Expecting type "application/json" or suffix "json".
> ---
> Backtrace:
> ▆
> 1. └─melodi::get_catalog()
> 2. └─httr2::resp_body_json(httr2::req_perform(httr2::request(url)), simplifyVector = TRUE)
> ```
This shows the package is using `httr2::resp_body_json` to something of type `text/html`. And indeed :

> ```R
> # testing url content type
> > httr2::resp_content_type(httr2::req_perform(httr2::request("https://api.insee.fr/catalog/all")))
> [1] "text/html"
> ```

It would be greatly appreciated to put some tests in this package so this does not happen on futur commits :)

Also, a quick fix would be great; probably have the package read the html, parse the content and translate it to json; but that being tried with :

> ```R
> # testing parsing xml to json so I can later parse the json
> > jsonlite::toJSON(
> xml2::read_xml(
> httr2::resp_body_string(
> httr2::req_perform(
> httr2::request("https://api.insee.fr/melodi/catalog/all")
> )
> )
> ),
> auto_unbox = TRUE,
> pretty = TRUE
> )
> ```

I get the error:

> ```R
> Erreur dans read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, :
> Opening and ending tag mismatch: link line 12 and head [76]
> 5. read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, options = options)
> 4. read_xml.character(httr2::resp_body_string(httr2::req_perform(httr2::request("https://api.insee.fr/melodi/catalog/all"))))
> 3. xml2::read_xml(httr2::resp_body_string(httr2::req_perform(httr2::request("https://api.insee.fr/melodi/catalog/all"))))
> 2. force(x)
> 1. jsonlite::toJSON(xml2::read_xml(httr2::resp_body_string(httr2::req_perform(httr2::request("https://api.insee.fr/melodi/catalog/all")))), auto_unbox = TRUE, pretty = TRUE)
> ```

which means something's off with the xml.

Maybe the xml should have some style information as seen from this screenshot

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.