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

Reply via email to