PUT calls via curl

Discuss any general programming issues here
Post Reply
afxgroup
Posts: 16
Joined: Mon Sep 12, 2016 6:54 pm

PUT calls via curl

Post 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
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: PUT calls via curl

Post 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
afxgroup
Posts: 16
Joined: Mon Sep 12, 2016 6:54 pm

Re: PUT calls via curl

Post 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
afxgroup
Posts: 16
Joined: Mon Sep 12, 2016 6:54 pm

Re: PUT calls via curl

Post by afxgroup »

I can confirm that :

e:setopt_put(True) hangs the call

e:SetOpt_CustomRequest("PUT") works
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: PUT calls via curl

Post by airsoftsoftwair »

What system are you on?
afxgroup
Posts: 16
Joined: Mon Sep 12, 2016 6:54 pm

Re: PUT calls via curl

Post by afxgroup »

sorry for the delay. I'm using Windows 10 (and now 11)
Post Reply