HTTP Post Body Parameters

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 2 03:08:14 UTC 2023


On 8/1/23 7:57 PM, Vahid wrote:
> Hi,
> 
> I want to submit a request to server with "x-www-form-urlencoded" 
> header. This is the simplified version of my code:
> 
> 
>      auto http = HTTP("https://myurl.com/api");
>      http.addRequestHeader("Content-Type", 
> "application/x-www-form-urlencoded");
>      http.addRequestHeader("Authorization", "SID:TOKEN");
> 
>      auto params = "Param1=one&Param2=two";
>      http.setPostData(params, "application/x-www-form-urlencoded");
> 
>      http.onReceive = (ubyte[] response)
>      {
>          return cast(string) response;
>      };

onReceive is a push from the library to you. Returning the response 
doesn't actually do anything. You need to store it somewhere.

e.g.

```d
ubyte[] msg;
http.onReceive = (ubyte[] response) {
     msg ~= response;
     return response.length; // tell the receiver how much data was read
};

http.perform(); // fills in msg
return msg;
```
from the std.net.curl docs about onReceive (which by the way, should 
return a ulong, I'm not sure how your cast to string even compiles):

The event handler that receives incoming data. Be sure to copy the 
incoming ubyte[] since it is not guaranteed to be valid after the 
callback returns.

-Steve


More information about the Digitalmars-d-learn mailing list