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

Provide option not to remove empty body fields in PUT/POST/PATCH #573

Closed
hongooi73 opened this issue Feb 18, 2019 · 2 comments
Closed

Provide option not to remove empty body fields in PUT/POST/PATCH #573

hongooi73 opened this issue Feb 18, 2019 · 2 comments

Comments

@hongooi73
Copy link

hongooi73 commented Feb 18, 2019

Something I just realized: PUT, POST and PATCH will silently remove empty fields in an encoded request body, due to the following line in body_config:

  if (!is.list(body)) {
    stop("Unknown type of `body`: must be NULL, FALSE, character, raw or list",
      call. = FALSE
    )
  }

  body <- compact(body)   # <---- right here

  # Deal with three ways to encode: form, multipart & json
  encode <- match.arg(encode)

Most of the time this is harmless, but sometimes the presence or absence of a field can be meaningful, eg an empty field means "delete this component" whereas leaving it out means "leave this component unchanged".

Can these verbs have an option to keep empty fields in the request body? Or if development work is switching to httr2, provide this option in the new package.

@cderv
Copy link
Contributor

cderv commented Feb 18, 2019

You can pass empty parts in body with httr if you use raw or character body.
There is a question on this in #561. Here some example code I have made there:

httr::POST("https://httpbin.org/post", body = '{"a":1,"b":{}}', encode = "raw")
#> Response [https://httpbin.org/post]
#>   Date: 2019-01-03 20:53
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 464 B
#> {
#>   "args": {}, 
#>   "data": "{\"a\":1,\"b\":{}}", 
#>   "files": {}, 
#>   "form": {}, 
#>   "headers": {
#>     "Accept": "application/json, text/xml, application/xml, */*", 
#>     "Accept-Encoding": "gzip, deflate", 
#>     "Connection": "close", 
#>     "Content-Length": "14", 
#> ...

or build the body directly using jsonlite

json_body <- jsonlite::toJSON(list(a = 1, b = NULL), auto_unbox = TRUE)
httr::POST("https://httpbin.org/post", body = json_body, encode = "raw")
#> Response [https://httpbin.org/post]
#>   Date: 2019-01-03 20:53
#>   Status: 200
#>   Content-Type: application/json
#>   Size: 464 B
#> {
#>   "args": {}, 
#>   "data": "{\"a\":1,\"b\":{}}", 
#>   "files": {}, 
#>   "form": {}, 
#>   "headers": {
#>     "Accept": "application/json, text/xml, application/xml, */*", 
#>     "Accept-Encoding": "gzip, deflate", 
#>     "Connection": "close", 
#>     "Content-Length": "14", 
#> ...

Created on 2019-01-03 by the reprex package (v0.2.1)

The encode = "raw" is not needed really because character body will stay as is, but it helps remember.
Using jsonlite externaly to httr to build the body is the main way to customise the body.

Could this fill your needs for empty body fields ?

@hongooi73
Copy link
Author

Ta, didn't realise this is a dup.

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