简体   繁体   中英

R httr content-type image

I'm making a POST request to this website, and I'm expecting an XML object in return. Using the httr package in R:

library("httr")
url <- "https://pathways.embl.de/mapping.cgi"
#body <- list(a = 1, b = 2, c = 3)
body <- list(selection = "R01324 W20 #ff0000", export_type="svg")

r <- POST(url, body = body, encode = "form", verbose())

This will return status_code 200 (success), but content(r) gives something like:

[1] 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 27 31 2e 30 27 20 65 6e 63 6f 64 69 6e 67 3d 27 49
  [32] 53 4f 2d 38 38 35 39 2d 31 27 3f 3e 0a 3c 21 44 4f 43 54 59 50 45 20 73 76 67 20 50 55 42 4c
  [63] 49 43 20 22 2d 2f 2f 57 33 43 2f 2f 44 54 44 20 53 56 47 20 31 2e 30 2f 2f 45 4e 22 20 22 68
  [94] 74 74 70 3...

(output truncated) which is definitely not xml.

Checking r$headers$content-type returns "image/svg+xml" .

I can do the equivalent in Python:

import requests

url = 'https://pathways.embl.de/mapping.cgi'
body = {"selection":"R01324 W20 #ff0000", "export_type":"svg"}

r = requests.post(url, data=body)

with open('test.svg','w') as file:
    file.write(r.text)

And this will successfully write out my desired xml output in test.svg .

I'd like to know how this can be done in R ?

比我想象的更容易修复: content(r, "text")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM