Page 1 of 1

PUT calls via curl

Posted: Thu Aug 11, 2022 7:09 pm
by afxgroup
Hi,
Anyone has tried a PUT (not POST) call via curl?

I've tried this function:

Code: Select all

Function p_executePUT(url, body, p_function)
	DebugPrint("CALLLING URL WITH PUT=", url)
	Local e = hurl.Easy()
	e:setopt_url(URL)
	e:setopt_put(True)
	e:setopt_httpheader({
		"Authorization: Bearer " .. AuthTable.ACCESS_TOKEN,
	});
	DebugPrint("postFields=", body)
	e:setopt_postfields(body)
	e:setopt_writefunction(p_function)
	e:setopt_followlocation(True)
	e:perform
	e:close	
EndFunction
But the program stuck most probably on perform and hangs
With POST I have no problem

Re: PUT calls via curl

Posted: Fri Aug 12, 2022 3:38 pm
by jPV
I have used a custom request to make PUT calls, I guess there was a reason to go this route, but can't remember details anymore :) Most likely it was some tip from generic mainstream coding forums for libcurl that lead to this. I think it wasn't because of hURL being the issue, but generic cURL stuff.

Here's what I did with some project:

Code: Select all

Const #CONTENTTYPE = "Content-Type: application/json"

Function p_SendRequest(method$, path$, body$)
    /*
    ** The internal DownloadFile() function only supports GET and POST methods, and a custom
    ** function using the hURL plugin is needed for other cases.
    ** param method$: Method string
    ** param path$: URI to send the request
    ** param body$: Message body in JSON
    ** return resp: Response body table
    ** return loc$: Location header string
    */

    Local e = hurl.Easy()
    Local response$ = ""
    Local err, resp, resp_code, loc$

    Switch LowerStr(method$)
    Case "put":
        e:SetOpt_HTTPHeader({#CONTENTTYPE})
        e:SetOpt_CustomRequest("PUT")
        e:SetOpt_PostFields(body$)
    Case "post":
        e:SetOpt_HTTPHeader({#CONTENTTYPE})
        e:SetOpt_CustomRequest("POST")
        e:SetOpt_PostFields(body$)
    Case "delete":
        e:SetOpt_HTTPHeader({#CONTENTTYPE})
        e:SetOpt_CustomRequest("DELETE")
    EndSwitch

    e:SetOpt_Timeout(10)
    e:SetOpt_ConnectTimeout(10)
    e:SetOpt_NoProgress(1)
    e:SetOpt_URL(g_settings.host .. path$)
    ; Write the received data to a local variable
    e:SetOpt_WriteFunction(Function(s$) response$ = response$ .. s$ EndFunction)
    ; Check if there's a location header and store it to the loc$ variable
    e:SetOpt_HeaderFunction(Function(s$) If StartsWith(s$, "Location:") Then loc$ = PatternFindStrShort(s$, "http://.-(/.+/)") EndFunction)

    ; Perform the request and catch any errors
    ExitOnError(False)
    e:Perform()
    err = GetLastError()
    ExitOnError(True)
    If err
        p_Error(GetErrorName(err) .. " (" .. err .. ")")
    Else
        resp_code = e:GetInfo_Response_Code()
    EndIf
    e:Close()

    ; Convert a JSON response to a table
    resp = DeserializeTable(response$)

    ; Display a message if the response code indicates an error
    If resp_code >= 400
        Local err$
        If HaveItem(resp, "@error")
            err$ = resp["@error"]["@message"] .. " (" .. resp_code .. ")"
            If resp_code > 400 Then err$ = err$ .. "\n\n" .. Concat(resp["@error"]["@messages"], "\n")
        ElseIf resp_code >= 500
            err$ = "Server error: " .. resp_code
        Else
            err$ = "Error: " .. resp_code
        EndIf
        p_Error(err$)
        resp = {}
    EndIf

    Return(resp, loc$)

EndFunction

Re: PUT calls via curl

Posted: Fri Aug 12, 2022 4:09 pm
by afxgroup
The funny thing is that only with PUT I have this problem. With GET and POST everythings works correctly.. I'll try with your method

Re: PUT calls via curl

Posted: Wed Aug 31, 2022 6:12 pm
by afxgroup
I can confirm that :

e:setopt_put(True) hangs the call

e:SetOpt_CustomRequest("PUT") works

Re: PUT calls via curl

Posted: Sat Sep 03, 2022 11:58 pm
by airsoftsoftwair
What system are you on?

Re: PUT calls via curl

Posted: Thu Nov 17, 2022 11:45 am
by afxgroup
sorry for the delay. I'm using Windows 10 (and now 11)