2

I have a question about to add body to GET request.

  1. In Postman, I easily add body raw json to tab Body

enter image description here

  1. In R, I use httr::GETand I can not find any option to add them. I try to use option query, but it return error 500 "Missing parameter username"

This is my code (sorry about fake data):

library(httr)

api <- ".........................../users/authorize"

query <- list("username": "xxxxx", "password": "xxxxxxxxxxxx")

resp <- GET(api, query = query)
stop_for_status(resp)
# Error: Internal Server Error (HTTP 500).

content(resp, type = "text", encoding = "utf-8")
# [1] "{\"status\":\"0\",\"error_code\":\"S002\",\"errors\":\"Missing param [username].\"}"

Can anybody help me to this case? Thank you so much.

Linh
  • 147
  • 7

1 Answers1

3

Try this:

resp <- httr::POST(body = query, encode = "json")
ekoam
  • 8,744
  • 1
  • 9
  • 22
  • Wow, it worked. Thank you. But I do not understand difference GET requests with body raw between Postman and R `httr::GET`? – Linh Oct 19 '20 at 17:07
  • 1
    Basically, a `GET` request with a body means a `POST` request in standard HTTP terminology (as used by `httr`). A real `GET` request has no data in its body -- you simply "get" data from the server via, for example, an URL. I guess that Postman tries to standardise all those requests into one type, but `httr` never does so. – ekoam Oct 19 '20 at 17:14
  • @Linh Hi, if this answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – ekoam Oct 20 '20 at 03:35
  • Dear @ekoam, I accepted your answer. Sorry for the late one. Thank for your enthusiasm. – Linh Oct 21 '20 at 04:32
  • Thank you @ekoam. That GET + body = POST was a game changer for me. – Eli Berkow Apr 16 '21 at 09:36