API using curlib

General FreeBASIC programming questions.
Post Reply
jaskin
Posts: 62
Joined: Sep 01, 2018 20:19

API using curlib

Post by jaskin »

I'm developing an API using the curlib and using the guidelines of the service provider to construct the necessary headers and data fields. The following code does nothing. What am I doing wrong?

Code: Select all

#include once "curl.bi"
#include once "crt/string.bi"

' this callback will be called when any data is received
Private Function write_callback CDecl _
    ( _
        ByVal buffer As Byte Ptr, _
        ByVal size As Integer, _
        ByVal nitems As Integer, _
        ByVal outstream As Any Ptr _
    ) As Integer

    Static As ZString Ptr zstr = 0
    Static As Integer maxbytes = 0

    Dim As Integer bytes = size * nitems

    'current zstring buffer too small?
    If( maxbytes < bytes ) Then
        zstr = Reallocate( zstr, bytes + 1 )
        maxbytes = bytes
    End If

    ' "buffer" is not null-terminated, so we must dup it and add the null-term
    memcpy( zstr, buffer, bytes )
    zstr[bytes] = 0

    ' just print it..
    Print *zstr

    Return bytes
End Function


' init
Dim As CURL Ptr curl = curl_easy_init( )
If( curl = 0 ) then end 1

' set url and callback
curl_easy_setopt(curl, CURLOPT_URL, "https://target.url")
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @write_callback)

'custom headers
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Content-Type: application/json; charset=UTF-8"" ""Accept: application/json; charset=UTF-8"" ""Version 2"" ""X-IG-API-KEY: aaaaaaa")

'specify we want to POST data
curl_easy_setopt(curl, CURLOPT_POST, 1L)

'specify the POST data */ 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "'{""identifier"": ""uuuuuuu"",""password"": ""ppppppp"",""encryptedPassword"": null}'")

'' execute..
curl_easy_perform(curl)

'' shutdown
curl_easy_cleanup(curl)
print "Done"
sleep

jaskin
Posts: 62
Joined: Sep 01, 2018 20:19

Re: API using curlib

Post by jaskin »

Found the problem after turning on verbose debugging. The problem is the proverbial:
SSL certificate problem: unable to get local issuer certificate

Now I can't figure out how to add the SSL certificate to the local environment. Please help!
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: API using curlib

Post by Imortis »

jaskin
Posts: 62
Joined: Sep 01, 2018 20:19

Re: API using curlib

Post by jaskin »

Thank you. Much appreciated. However I experienced other issues such as FB crashing. I've now moved on to Python where doing such work is so much simpler and easier. Sad since I love using FB - have been as far back as I can recall. I will continue using it for most other purposes but when it comes to networking and the like Python makes it so easy it's not funny. 'll probably try to do in all in FB when I have the time but for now I have an urgent need to get something up and running.
Post Reply