Ask Your Question
2

How to make an httr SOAP API request using R?

asked 2021-06-11 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-07-30 10:00:00 +0000

huitzilopochtli gravatar image

To make an httr SOAP API request using R, follow these steps:

  1. Install the httr and XML packages in R by using the following commands in R console:
install.packages("httr")
install.packages("XML")
  1. Load the httr and XML packages in R.
library(httr)
library(XML)
  1. Set the URL for the SOAP API.
url <- "http://example.com/soap-api"
  1. Create the SOAP envelope.
soap_body <- '<?xml version="1.0" encoding="utf-8"?>
              <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                  <soap:Body>
                      <myFunctionRequest xmlns="http://example.com/">
                          <parameter1>value1</parameter1>
                          <parameter2>value2</parameter2>
                      </myFunctionRequest>
                  </soap:Body>
              </soap:Envelope>'

Note: Replace the myFunctionRequest and parameter1, parameter2 with your desired function and parameters.

  1. Make the SOAP API request using the httr package.
response <- POST(url,
                 body = soap_body,
                 add_headers("Content-Type" = "text/xml;charset=UTF-8",
                             "SOAPAction" = "http://example.com/myFunction"),
                 verbose())

Note: Replace the "http://example.com/myFunction" with your desired function.

  1. Extract the response data from the API.
response_content <- content(response, as = "text")
response_xml <- xmlParse(response_content)
result <- xpathApply(response_xml, "//myResult", xmlValue)

Note: Replace "//myResult" with your desired result node.

  1. Print the result.
print(result)

Your output will contain the required information from the SOAP API request.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-11 11:00:00 +0000

Seen: 20 times

Last updated: Jul 30 '21