r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

Can't POST image usign httr #724

Closed achaldixit closed 1 year ago

achaldixit commented 2 years ago

Looking for help to figure out how can I send images using httr::POST on google chat. So google chat provides a webhook to which we can POST using a HTTP request.

CHAT_URL <- "https://chat.googleapis.com/v1/spaces/XXXXXXXXX"

#test_image = upload_file("~/R_codes/PnL_Bot/testimage.png")

POST(url = CHAT_URL,
     add_headers(`Content-Type`="multipart/form-data"),
           body = list(y = upload_file("~/XXX/testimage.png")),
     encode = "multipart")

I am getting the following error response:

Response [https://chat.googleapis.com/v1/spaces/XXXXXX]
  Date: 2022-09-27 10:12
  Status: 400
  Content-Type: application/json; charset=UTF-8
  Size: 114 B
{
  "error": {
    "code": 400,
    "message": "Message cannot be empty.",
    "status": "INVALID_ARGUMENT"
  }
}

For texts, it works fine! Thanks in advance.

jennybc commented 2 years ago

I remember that there is something peculiar about uploading media to Google Drive. Perhaps it is the same for this API? I haven't thought about this in a while, but maybe something here will help you:

https://github.com/tidyverse/googledrive/blob/0297c7609ce997dabc0228616ddc478a9ff05386/R/drive_update.R

If you really have to troubleshoot this, use the API explorer to send a successful request and capture exactly how that goes out over the wire. Then run your failing request with httr::with_verbose(). Figure out how to make the latter look like the former.