MS-Tech
26th July 2021, 14:11
Hallo Zusammen,

ich möchte eine REST Api über den HTTP Client anbinden. Aktuell würde ich gerne via PUT oder PUSH Daten zu versenden. In der Hilfe ist leider nur ein GET Beispiel enthalten und ich weiß einfach nicht, wie ich die zu sendenden Daten als JSON-Objekt bzw. JSON-String mitzugeben.

Beispiel aufbau JSON-Objekt:

| JSON erstellen
json.item = Json.newObject()
Json.setString(json.item, "DataA", "")
Json.setString(json.item, "DataB", "")
Json.setString(json.item, "DataC", "")
Json.setString(json.item, "DataD", "")


Danach schreibe ich das JSON-Objekt in einen String, um diesen dann über den HTTP-Client an die API zu übergeben.


| JSON in String schreiben
Json.writeString(json.item, json.text)


Im Anschluss daran rufe ich meine Methode auf, um dann den Request abzusezten.


| REST Schnittstelle aufrufen und JSON Text mitgeben
if not httpclt.put(strip$(l.url), json.text, l.error)
....
endif


Die Funktion sieht folgendermaßen aus:



function boolean http.put(const string i.url, string i.data, ref string errormsg())
{
long response
long statuscode
string statustext(128)
long curlcode
long bodystream
long jsonvalue

|* Perform an HTTP PUT request
response = http.put( i.url,
HTTP_ACCEPT,
"application/json",
i.data)

|* Get the HTTP status code; for example:
|* 200 [HTTP_STATUS_OK] means the request was processed successfully
|* 400 [HTTP_STATUS_BAD_REQUEST] means the request was not understood
|* 500 [HTTP_INTERNAL_SERVER_ERROR] means the server encountered an error while processing
statuscode = http.response.statuscode(response)

|* Get an HTTP status text, like "OK", "Bad Request", "Internal Server Error" etc.
statustext = http.response.statustext(response)

if statuscode <> 200
or trim$(statustext) <> "OK" then
errormsg = statustext
return(false)
endif

|* Cleanup, this will also delete the HTTP headerlist; also the body stream is closed
http.response.delete(response)

return(true)
}


Ich weiß nur absolut nicht, ob die Datenübergabe in der Funktion http.put(..) so funktioniert (siehe fett markierter Bereich).

Hat jemand von euch einen Tipp?

Viele Grüße
MS-Tech

bdittmar
26th July 2021, 17:11
Hallo,

lt. Hilfe.

http.put()
Syntax:
#include <bic_httpclt>

function long http.put (const string url, ...)

Description

Sends a PUT HTTP request to a URL.

Arguments
const string url the URL to send the request to

... see http.send() for more information.

Return values
an http.response object; check the response object for information about whether the request succeeded

Context
This function is implemented in the 4GL Tools and can be used in all script types. This function is available from TIV level 2120.

Preconditions
passed number of arguments must be valid
passed argument types must be valid
passed attributes/flags must be known
Related topics

HTTP Client overview
HTTP Client synopsis