SOAP Client with WSDL for R - r

I'm trying to write a code for a SOAP client with R using the SSOAP package. This was my inicial code:
wsdl <- getURL("http://sistemas.cvm.gov.br/webservices/Sistemas/SCW/CDocs/WsDownloadInfs.asmx?WSDL")
def <- processWSDL(doc, verbose = TRUE)
ff <- genSOAPClientInterface(def = def, verbose = TRUE)
But I think the WSDL documentation is too complex (multi-dimensional) for the functions. I tried (this and many other things) to simplify the WSDL choosing just one service, and it helped me with the processWSDL function, but I cannot generate the client functions yet. The error message is:
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
Please, could somebody help me?

The RCurl package helps us to do this (see an example on http://www.stat.wvu.edu/~jharner/courses/stat623/docs/RCurlJSS.pdf):
library(RCurl)
library(XML)
###############
#### Login ####
###############
headerfields = c(
Accept = "text/xml",
Accept = "multipart/*",
'Content-Type' = "text/xml; charset=utf-8",
SOAPAction = "http://www.cvm.gov.br/webservices/Login"
)
body = "<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Header>
<sessaoIdHeader xmlns='http://www.cvm.gov.br/webservices/'>
<Guid>8200ac01-bfb5-46d6-a625-38108141fb33</Guid>
<IdSessao>135128883</IdSessao>
</sessaoIdHeader>
</soap:Header>
<soap:Body>
<Login xmlns='http://www.cvm.gov.br/webservices/'>
<iNrSist>XXXX</iNrSist>
<strSenha>XXXXX</strSenha>
</Login>
</soap:Body>
</soap:Envelope>"
reader = basicTextGatherer()
curlPerform(
url = "http://sistemas.cvm.gov.br/webservices/Sistemas/SCW/CDocs/WsDownloadInfs.asmx",
httpheader = headerfields,
postfields = body,
writefunction = reader$update
)
xml <- reader$value()
xml
You have to do something like this to each one of the fuctions in http://sistemas.cvm.gov.br/webservices/Sistemas/SCW/CDocs/WsDownloadInfs.asmx
If you have something easier (or more elegant) and want to share will be welcome!
Tks!

Related

Error when trying to prase a HTTP-Request in R

im using R package httr to get a HTTP-Response for a specific link.
When trying to parse the content of the response im getting the Error:
Fehler in parse(text = script_content) : <text>:1:10: Unerwartete(s) '['
1: {"lines":[
Translated to enlgish it says something like this (sorry for my error messages being in German):
Error in parsing(text = script_content) : <text>1:10: Unexpected '['
1: {"lines":[
It seems as there is a problem with the format/encoding of the text. Here is my code:
script <-
GET(
url = "https://my_url.which_origin_is_not_important/my_script.R",
authenticate(username, pass)
)
script_content <- content(script, as = "text", encoding = "ISO-8859-1")
parsed_condent <- parse(text = script_content )
The value of script_content looks like this:
"{\"lines\":[{\"text\":\"################## FUNCTION ##################\"},{\"text\":\"\"},{\"text\":\"library(log4r)\"}],\"start\":0,\"size\":32,\"isLastPage\":true,\"limit\":500,\"nextPageStart\":null}"
Some more background to this operation: Im trying to source a code, which is currently inside of a private repository. I wrote the code myself i'm trying to source. I made sure, that the issue is not coming from within th code.
I got the solution from: Sourcing R files in a private github folder
Thanks for any advice!!

Using RCurl, how to download the "clone from github" zip file?

If I want to download a clone as a zip, it does a redirect.
zip.url = "https://github.com/MonteShaffer/humanVerse/archive/refs/heads/main.zip"
redirects to:
<html><body>You are being redirected.</body></html>
I am trying to using the RCurl library:
require(RCurl)
curl.fun = basicTextGatherer();
curl.ch = getCurlHandle();
x = getBinaryURL(zip.url, curl = curl.ch, headerfunction = curl.fun$update )
One windoze 10, throwing this error:
Error in function (type, msg, asError = TRUE) :
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
I am assuming github is doing multiple redirects. I want to download the file as a binary 'zip'.
You have to set the curl option followlocation to TRUE, like this:
binary_blob <- RCurl::getBinaryURL(zip.url, .opts = list(followlocation = TRUE))
It might be easier to download the file instead with the following two options:
utils::download.file() comes with R and works for this.
zip.url <- "https://github.com/MonteShaffer/humanVerse/archive/refs/heads/main.zip"
download.file(zip.url, "main.zip")
The curl package has curl_download().
library(curl)
curl::curl_download(zip.url, "main2.zip")

httr POST request error: upstream connect error or disconnect/reset before headers. reset reason: connection termination

I am currently trying to upload a CSV file to a proprietary service via httr::POST(). Unfortunately the Admins are not experienced in R and can give only little support.
This is an example on how it should look like in the command line:
curl -X POST --header 'Content-Type: multipart/form-data' \
-F file=#"member-1-test.csv.gz" 'https://some/api/endpoint'
So, in the following code I just try to stick with the example (and additionally provide a token).
> library(tidyverse)
> library(httr)
# Provide some test data with characters specifically quoted
> test_file <- tibble::tribble(
~keytype, ~key, ~action, ~segment,
6,"\"https://www.google.com\"", 0, 37372818,
6,"\"https://www.sport1.de\"" , 0, 37372818
)
> data.table::fwrite(test_file, "test.csv", quote = FALSE)
> file <- upload_file(path = "C:/R/projects/DefaultWorkingDirectory/test.csv")
> res <- POST(
url = "https://some/api/endpoint",
body = list(file = file),
add_headers(.headers = c('Content-Type' = "multipart/form-data", "Authorization" = token))
)
This gives me the follwing error:
> res
Response [https://some/api/endpoint]
Date: 2020-11-18 09:35
Status: 503
Content-Type: text/plain
Size: 95 B
> content(res, encoding = "UTF8")
"upstream connect error or disconnect/reset before headers. reset reason: connection termination"
Any help or guidance on how to move forward with this issue is very much appreciated. Thanks!
Directly after posting the question it was already solved by one of the Admins :)
The issue was that the encoding needs to be set to "multipart" and that a specific content type needs to be provided which is similar to JSON but needs to be added to the "Accept"-header field.
Here the answer for any future references:
> res <- POST(
url = "https://some/api/endpoint",
body = list(
file = upload_file("test.csv")
),
add_headers(c(
"Authorization" = token,
"Accept" = "specific_format_for_the_application"
)),
encode = "multipart",
verbose()
)

R: How to save the content of a curl request as ascii text?

As a follow on from: How translate this curl command into a R curl call?
I can successful do a curl request but the returned result looks to be in a binary format, but the content should be ASCII. How do I write it out as an ASCII file?
require(httr)
headers = c(
`Content-Type` = 'text/csv'
)
data = upload_file('data/data.csv')
res <- httr::POST(url = 'https://some.url.com/invocations', httr::add_headers(.headers=headers), body = data)
content(res, "text")
works and if the output is JSON then
content(res, "parsed")
works too.

Internal server error using Rcurl

I want to use the following curl command using RCurl
curl -X POST http://test.reco4j.org:7474/db/data/ext/Reco4jRecommender/node/248/get_recommendations -H "Content-Type: application/json" -d '{"type":"0"}'
so i am using the following R code
library(RCurl)
library(RJSONIO)
postForm("http://test.reco4j.org:7474/db/data/ext/Reco4jRecommender/node/248/get_recommendations",
.opts = list(postfields = toJSON(list(id = "0")),
httpheader = c('Content-Type' = 'application/json', ssl.verifypeer = FALSE)
))
But I get an "Internal server errror", so i am not sure my R code is wrong or it is a windows problem.
The reason I am mentioning this, is that the original curl command fails in windows but works on mac and Linux, so I am not sure the R failure is a windows issue or an R issue.
You have an error in your code. The pair you need to send is "type":"0" you are sending "id":"0".
library(RCurl)
library(RJSONIO)
res <- postForm("http://test.reco4j.org:7474/db/data/ext/Reco4jRecommender/node/248/get_recommendations",
.opts = list(postfields = toJSON(list(type = "0")),
httpheader = c('Content-Type' = 'application/json', ssl.verifypeer = FALSE)
))
out <- fromJSON(rawToChar(res))
> head(out[[1]])
$outgoing_relationships
[1] "http://test.reco4j.org:7474/db/data/node/2285/relationships/out"
$data
$data$movieId
[1] 1342
$data$title
[1] "Convent, The (Convento, O) (1995)"
$data$releaseDate
[1] "14-Jun-1996"
It looks like a bad URL. I get an HTTP ERROR 500: INTERNAL_SERVER_ERROR trying to access that URL in firefox.
EDIT: Disregard, you're right: the curl command worked in a shell prompt. Sorry for doubting you.

Resources