2

I am trying to pass a json array in a POST request using the httr package. However I get a HTTP/1.1 400 Bad Request because of the formatting of the array. Here's how I build the request:

decos <- list(name="ais-static-journey")
decos <- jsonlite::toJSON(decos, auto_unbox = TRUE) #Have also tried decos <- toJSON(decos, pretty = TRUE)

body <- list(mmsis=mmsis, startDate= startDate,
             endDate= endDate, decorations = decos)

r <- POST(decoratedurl,body=body, encode="json",add_headers(Authorization=auth), verbose())

I also had a look at this.

The error I get is primarily because of the back slashes in the request:

"decorations":"{\"name\":\"ais-static-journey\"}"}

This needs to be "decorations":[{"name":"ais-static-journey"}] How do I achieve this?

Dhiraj
  • 1,650
  • 1
  • 18
  • 44

1 Answers1

2

Figured out. This works:

decos <- list(list(name="ais-static-journey"))
body <- list(mmsis=mmsis, startDate= startDate,
             endDate= endDate, decorations = decos)
r <- POST(decoratedurl,body=body, encode="json",add_headers(Authorization=auth), verbose())
Dhiraj
  • 1,650
  • 1
  • 18
  • 44