#2872 GET request with a body

Wesley Thu 30 Jun 2022

Hi all,

I have been struggling with getting a WebClient to execute a GET request with a (typed) request body.

The case is as follows:

We have an API we want to collect data from. This API expects a GET request with a Body (containing information what to request such as an id) and the Content-Type application/json.

Now I have done multiple attempts, using the writeStr and readRes funcs in combination with resStr and getStr for example.

I noticed that when I use the Content-Type header in combination with the getStr func the request will hang and eventually time-out, and if I use the Content-Type header along with readRes.resIn.readAllStr it will result in an empty response.

So far all my attempts have resulted in either a time-out or an empty result (which, I guess, is caused by the body not being sent, however I have not confirmed this as of now).

Does anyone know the best way to approach this problem and perhaps shed some light on what I am doing wrong here?

Best regards,


Henry Thu 30 Jun 2022

Hi Wesley,

In order to write data to the body of a request via WebClient you'll need to use the reqOut method to write to the outstream of the request before running readRes

I'd recommend using writeReq in combination with reqOut to ensure the headers you're setting on WebClient aren't being overwritten by methods like writeStr

brian Thu 30 Jun 2022

Take a look at the code for writeStr which can be used. Or drop down and use writeReq as suggested by Henry. Here is simple example:

c := WebClient(uri)
c.writeStr("GET", "request body")
if (resCode != 200) throw IOErr("Bad HTTP response $c.resCode")
resStr := c.resIn.readAllStr

Login or Signup to reply.