0

I want to post xml with R, the code in python is

import urllib2

url = 'http://www.rcsb.org/pdb/rest/search'

queryText = """
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>
"""

print "query:\n", queryText
print "querying PDB...\n"
req = urllib2.Request(url, data=queryText)
f = urllib2.urlopen(req)
result = f.read()

if result:
    print "Found number of PDB entries:", result.count('\n')
else:
    print "Failed to retrieve results" 

now, I want to use R to finish the same function, How to do.

I have tried several times.

library(RCurl)
library(httr)
library(XML)

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>

<orgPdbQuery>

<version>B0907</version>

<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>

<description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>

<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>

</orgPdbQuery>'

# first try ----
xml_txt <- xmlTreeParse(xml_text,useInternalNodes=T)
postForm(url1, "xml"=saveXML(xml_txt), style="post")
#failed

"Problem creating Query from XML: Content is not allowed in prolog.\nxml=\n\n B0907\n org.pdb.query.simple.ExpTypeQuery\n Experimental Method Search : Experimental Method=SOLID-STATE NMR\n SOLID-STATE NMR\n\n\n" attr(,"Content-Type") charset "text/plain" "ISO-8859-1"

# second try ----
xml_out <- 'tmp.xml'
saveXML(xml_txt, xml_out)
result <- POST(url1, body = list(x = upload_file(xml_out)), encode = 'multipart', )
content(result)
# failed

return the website html code.

# 3rd try ----
httpPOST(url1, content = xml_txt)
# failed

"!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">http://www.w3.org/1999/xhtml\">Error

Error

ErrorThis page can't be displayed. Contact support for additional information.
The incident ID is: N/A.
# 4th try ----
h = basicTextGatherer()
result <- curlPerform(url = url1,
  httpheader=c(Accept="text/xml", Accept="multipart/*",
    'Content-Type' = "text/xml; charset=utf-8"),
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value

# failed

result

OK

0

h$value()

[1] ""


I have solved the problem.

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>
  <orgPdbQuery>
  <version>B0907</version>
  <queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
  <description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
  <mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
  </orgPdbQuery>'
h = basicTextGatherer()
httpheader=c(Accept="*/*",
  "Content-Type"="application/x-www-form-urlencoded")

result <- curlPerform(url = url1,
  httpheader=httpheader,
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value()
Kang Li
  • 109
  • 9
  • Is this the valid url? – user227710 May 28 '15 at 18:06
  • I hanve changed to the real code. – Kang Li May 29 '15 at 00:56
  • Kang, you should check out the `httr` and `XML` package vignettes, they will give you a good start. Once you have a more refined question (other than, "do it for me in R"), we can help you. Voting to close until there's a more specific question. – Brandon Bertelsen May 29 '15 at 01:43
  • Thank you. I have try the function of POST, POST, postForm, curlPerform, and I had browsered the websites such as http://stackoverflow.com/questions/15240408/how-to-send-an-xml-request-to-an-api-with-r http://stackoverflow.com/questions/24050773/rcurl-with-posting-data-using-xml http://stackoverflow.com/questions/26706159/post-xml-form-using-httr but I can't solve the problems – Kang Li May 29 '15 at 02:08
  • help , help, please, please, @Brandon – Kang Li May 29 '15 at 15:00
  • I'm voting to reopen the question because there is now enough to work with. Is all you're expecting from the query the result count? – Brandon Bertelsen May 29 '15 at 20:51
  • `curl 'http://www.rcsb.org/pdb/search/smartRow.do' --data 'smartSearchSubtype=ExpTypeQuery&mvStructure.expMethod.exclusive=n&mvStructure.expMethod.value=SOLID-STATE+NMR&r=0&target=Current' --compressed` – Brandon Bertelsen May 29 '15 at 21:04
  • @Brandon, thank you. That's a good idea, but I want to write a R package to finish this work, so .... – Kang Li May 30 '15 at 00:26
  • I'm just putting it there so I remember what I planned to write when the question is reopened. Either way, you can see what the return value is, my plan will be to just program that curl command and then string parse the result. Nothing really fancy. – Brandon Bertelsen May 30 '15 at 01:54
  • 1
    @Brandon, thanks for your help, I have solved this problem by changing the httpheader in the function of RCurl::curlPerform. httpheader=c(Accept="*/*", "Content-Type"="application/x-www-form-urlencoded") – Kang Li May 30 '15 at 02:06
  • @Brandon, thanks for your help.My English is poor and I am new in the stackoverflow.com, but I still express my gratitude. – Kang Li May 30 '15 at 02:15
  • If you have found a solution to your problem, you should add it as an answer for others who have a similar question. – Brandon Bertelsen May 30 '15 at 04:40

1 Answers1

3

I have solved the problem.

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>
  <orgPdbQuery>
  <version>B0907</version>
  <queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
  <description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
  <mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
  </orgPdbQuery>'
h = basicTextGatherer()
httpheader=c(Accept="*/*",
  "Content-Type"="application/x-www-form-urlencoded")

result <- curlPerform(url = url1,
  httpheader=httpheader,
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value()
Kang Li
  • 109
  • 9