Question

I have to make a post request. The problem is, the application that is exposing it's api as webservice, has set it's Content-Type to "text/plain". I tried RCurl package and httpRequest package but could not figure out how to set the content-type. I tried to directly make a postrequest to a URL that did not have that content-type, then postForm worked. Any help here?

EDIT: I will try to explain what I need to do here. I have an url. I need to make POST requests to it by passing it some parameters something like this

POST to "url/agents/where/agent?="Smith"

This is what you would type if you use the REST console extension on chrome for making webservice calls. I need to make this from R by setting the content-type to "text/plain"

What I have tried is this,

postForm("url/agents/get/all")

This works like a charm as say in our case the webservice call for calling all the agents does not require the content-type to be "text/plain" and also does not require any parameters to be passed to it(eg. no particular agent name as I want all of them). I think I have figured out how to pass a parameter by giving name value pairs in a list to .opts. What I am not able to figure out is how to set content-type.

I have tried this but it failed,

postForm("url/agents/where",
         .opts = list(postfields = toJSON(list("name" = "Smith")),
         httpheader = c('Content-Type' = 'text/plain', ssl.verifypeer = FALSE)))
Was it helpful?

Solution

It would be helpful to have the code that you've tried, but the general answer is that you can specify it in the httpheader argument to curlPerform. Here's a slightly modified version from the documentation:

h <- basicTextGatherer()
h$reset()
curlPerform(url,
            httpheader=c('Content-Type' = "text/plain"),
            postfields=toJSON(list("name" = "Smith")),
            customrequest = 'POST', 
            writefunction = h$update,
            verbose = TRUE,
            ssl.verifypeer = FALSE)
body <- h$value()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top