Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding content_type_json() to a POST no longer works with v0.5 #140

Closed
wligtenberg opened this issue Sep 3, 2014 · 7 comments
Closed

adding content_type_json() to a POST no longer works with v0.5 #140

wligtenberg opened this issue Sep 3, 2014 · 7 comments

Comments

@wligtenberg
Copy link

I used to create a POST as follows:
POST(url = <URL>, body = <some json>, content_type_json())

However, using v0.5 this no longer works, however using:
POST(url = <URL>, body = <some json>, add_headers('Content-Type' = "application/json"))
Does work.

@hadley
Copy link
Member

hadley commented Sep 3, 2014

Could you please provide a reproducible example?

(Also you can now do POST(..., encode = "json"))

@wligtenberg
Copy link
Author

Unfortunately, the API I am programming against is not publicly available.
I will have a look at the encode = "json", and report back if that resolves my issues.

@wligtenberg
Copy link
Author

unfortunately, I also cannot use the encode = "json" version, because that generates the wrong JSON.
It does set the Content-type correctly to application/json.
However, it creates arrays for every item in the list, which RJSONIO does not.
E.g.
RJSONIO generates:
{"properties":{"id":"id2","name":"MyName","age":"20"}}
encode = "json" generates:
{"properties":{"id":["id2"],"name":["MyName"],"age":["20"]}}
This then results in a bad request.

@hadley
Copy link
Member

hadley commented Sep 3, 2014

Yeah, you'd have to use unbox(). But I don't understand why content_type_json() doesn't work for you:

The output is identical for me:

> httr::content_type_json()
Config: 
List of 1
 $ httpheader:"application/json"
  ..- attr(*, "names")="Content-type"
> httr::add_headers('Content-Type' = "application/json")
Config: 
List of 1
 $ httpheader:"application/json"
  ..- attr(*, "names")="Content-Type"

(modulo case, which doesn't matter, and hasn't changed in 0.5)

@wligtenberg
Copy link
Author

The output is the same for me as well, however, when I use it in POST it does not behave the same.

If I put unbox() around all the things nested in the list, it works.
However, it is a lot easier for me, to just use RJSONIO for the JSON conversion and then use: add_headers('Content-Type' = "application/json") for now.

@hadley hadley closed this as completed Sep 3, 2014
@wligtenberg
Copy link
Author

Hi Hadley,

I think I have found the problem. I will see if I can fix it myself.
But that might be tomorrow.
If the body is a character (which is in my case) the body_raw function is called as follows:

if (is.character(body) || is.raw(body)) {
  return(body_raw(body))
}

which results in "Content-type" being set to "", because type is NULL.

@hadley
Copy link
Member

hadley commented Sep 3, 2014

Oh hmmm, so the reason that content_type_json() doesn't work is actually a bug in how headers are overrdien.

@hadley hadley reopened this Sep 3, 2014
@hadley hadley closed this as completed in bddf2f1 Sep 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants