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