POST request with std.net.curl

bachmeier no at spam.net
Fri Jul 23 21:25:01 UTC 2021


On Friday, 23 July 2021 at 19:59:33 UTC, Ali Çehreli wrote:
> On 7/23/21 11:11 AM, bachmeier wrote:
>> I'm writing a D program that interacts with the Todoist API 
>> using std.net.curl. It's not a problem to do get requests to 
>> query tasks, but I cannot find a way to get post to work to 
>> create a new task.
>> 
>> This is a working bash script, where APIKEY is defined 
>> elsewhere and $1 and $2 are user input when running the script:
>> 
>> ```
>> curl "https://api.todoist.com/rest/v1/tasks" -X POST --data 
>> "{"'"'"content"'"'": "'"'"$1"'"'", "'"'"project_id"'"'": $2}" 
>> -H "Content-Type: application/json" -H "Authorization: Bearer 
>> $APIKEY"
>> ```
>> 
>> (For obvious reasons I'm not going to provide a working 
>> example.) How can I translate this into a post function call 
>> within D?
>> 
>> [Todoist API 
>> Reference](https://developer.todoist.com/rest/v1/?shell#create-a-new-task)
>
> I haven't used it but isn't it the version of post that takes a 
> dictionary here?
>
>   https://dlang.org/phobos/std_net_curl.html#.post
>
> If the Authorization is the problem, I wonder whether 
> addRequestHeader is the solution:
>
>   
> https://dlang.org/phobos/std_net_curl.html#.HTTP.addRequestHeader
>
> Ali

Authorization is working - it's the same whether I'm doing a GET 
or POST request. The problem is passing the data. The main 
problem is that the documentation doesn't explain how to 
translate a `--data` option into a `post` call. I've tried 
everything I can think of, including what's shown in the 
documentation, but haven't found anything that works.


More information about the Digitalmars-d-learn mailing list