www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - request assistance resolving curl error

reply confuzzled <con fuzzled.com> writes:
Hello all,

I have two scripts. I copied the first directly from the alpaca 
website and massaged it with etc.c.curl until it compiled in D. 
The result is that it creates the order and returns the result to 
stdout. In the second script, I tried to use std.net.curl but 
cannot get it to work.

The only difference I can recognize between the two is that in 
the direct C version, CurlOption.writedata accepts a file handle 
and is passed stdout as in the example presented on alpaca's 
website while the wrapper accepts three different types (i.e., 
const(char)[], long, void*) and regardless of what I pass to it, 
nothing works. I tried to not setting it but that didn't work 
either. Don't know if that is even an option since alpaca_c did 
not work without it and resulted in the same error 
`{"code":40010000,"message":"request body format is invalid"}`.


**1. alpaca_c**

```d
module alpaca_c;

import etc.c.curl;
import core.stdc.stdio: stdout;
import std.conv: text;

import config;

pragma(lib, "curl");


int main()
{
     CURL *hnd = curl_easy_init();

     curl_easy_setopt(hnd, CurlOption.customrequest, "POST".ptr);
     curl_easy_setopt(hnd, CurlOption.writedata, stdout);
     curl_easy_setopt(hnd, CurlOption.url, 
"https://paper-api.alpaca.markets/v2/orders".ptr);

     curl_slist *headers = null;
     headers = curl_slist_append(headers, "accept: 
application/json");
     headers = curl_slist_append(headers, "content-type: 
application/json");
     headers = curl_slist_append(headers, i"APCA-API-KEY-ID: 
$(config.api_key)".text.ptr);
     headers = curl_slist_append(headers, i"APCA-API-SECRET-KEY: 
$(config.secret_key)".text.ptr);
     curl_easy_setopt(hnd, CurlOption.httpheader, headers);

     curl_easy_setopt(hnd, CurlOption.postfields, 
`{"side":"buy","type":"market","time_in_force":"day","qty":"1","symbol":"LCID"}`.ptr);

     CURLcode ret = curl_easy_perform(hnd);

     return 0;
}
```

**2. alpaca_d**

```d
import std.net.curl: Curl;
import etc.c.curl: curl_slist, curl_slist_append, CurlOption;
import core.stdc.stdio: stdout;
import std.stdio: writeln;
import std.conv: text;

import config;

pragma(lib, "curl");

void main()
{
     string result;

     Curl c;
     c.initialize();
     c.set(CurlOption.customrequest, "POST");
     c.set(CurlOption.writedata, cast(void*) stdout);
     c.set(CurlOption.url, 
"https://paper-api.alpaca.markets/v2/orders");
     string key = i"APCA-API-KEY-ID: $(config.api_key)".text;
     string secret = i"APCA-API-SECRET-KEY: 
$(config.secret_key)".text;
     curl_slist* headers;
     headers = curl_slist_append(headers, "accept: 
application/json");
     headers = curl_slist_append(headers, "content-type: 
application/json");
     headers = curl_slist_append(headers, &key[0]);
     headers = curl_slist_append(headers, &secret[0]);
     c.set(CurlOption.httpheader, headers);
     c.set(CurlOption.postfields, 
`{"side":"buy","type":"market","time_in_force":"day","symbol":"LCID","qty":"1"}`);
     c.onReceive = (ubyte[] data) { result ~= data; return 
data.length; };
     auto ret = c.perform();

     writeln(result);
}
```

Thanks for your assistance.


--confuzzled
Mar 26
parent reply Andrea Fontana <nospam example.org> writes:
On Tuesday, 26 March 2024 at 07:13:24 UTC, confuzzled wrote:
 Hello all,

 I have two scripts. I copied the first directly from the alpaca 
 website and massaged it with etc.c.curl until it compiled in D. 
 The result is that it creates the order and returns the result 
 to stdout. In the second script, I tried to use std.net.curl 
 but cannot get it to work.
I think you should use the HTTP interface, did you check this docs? https://dlang.org/phobos/std_net_curl.html#.HTTP https://dlang.org/phobos/std_net_curl.html#.HTTP.addRequestHeader Andrea
Mar 26
parent confuzzled <con fuzzled.com> writes:
On 3/26/24 8:44 PM, Andrea Fontana wrote:
 On Tuesday, 26 March 2024 at 07:13:24 UTC, confuzzled wrote:

 I think you should use the HTTP interface, did you check this docs?
 https://dlang.org/phobos/std_net_curl.html#.HTTP
 https://dlang.org/phobos/std_net_curl.html#.HTTP.addRequestHeader
 
 
 Andrea
Thanks Andrea. I noticed it and even tried HTTP before but could not find a means for setting CurlOptions. As a result, I chose to stick with Curl. Following your comment, however, I went back and combed the source and found it buried in HTTP.handle. Thanks again. --confuzzled
Mar 27