Hey.
In my Roko channel I am sending a POST request to our server.
Is there a way to add params to POST request body (Not the header)?
It can be a single key-value or several of them. or a json object
Thanks,
dan_shneider wrote:Thanks, but in this case, can you please explain how do I add header params and body params (json) to a request/file?
belltown wrote:To add POST params to the request body, use ifUrlTransfer.AsyncPostFromString(request), where request is a string containing your POST parameters, url-escaped, separated by ampersands. To url-escape a parameter, call ifUrlTransfer.Escape(param).
EnTerr wrote:belltown wrote:To add POST params to the request body, use ifUrlTransfer.AsyncPostFromString(request), where request is a string containing your POST parameters, url-escaped, separated by ampersands. To url-escape a parameter, call ifUrlTransfer.Escape(param).
Oh, my bad for glancing past *PostFromString()! (what was i thinking? probably the query string but that's part of the URL)
The non-Async version should work too, right?
Assuming one does not care about the response body (e.g. as often used as a REST action button).
Value1="some value"
Value2="some other value"
post="{"+Chr(34)+"Key1"+Chr(34)+":"+Chr(34)+Value1+Chr(34)+","+Chr(34)+"Key2"+Chr(34)+":"+Chr(34)+Value2+Chr(34)+"}"
If http.AsyncPostFromString(post) Then
event = Wait(timeout, http.GetPort())
If Type(event) = "roUrlEvent" Then
response = event.GetString()
Else If event = invalid Then
? "AsyncPostFromString timeout"
http.AsyncCancel()
Else
? "AsyncPostFromString unknown event", event
End If
End If
if response <> invalid AND response <> ""
json = parseJSON(response)
tim_beynart wrote:Using JSON formatting functions will not work, you have to generate your POST JSON manually using Chr(34) to insert the quotation marks.
Value1="some value"
Value2="some other value"
a={}
a["Key1"]=Value1
a["Key2"]=Value2
? FormatJSON(a)
'==>
{"Key1":"some value","Key2":"some other value"}
Tim, can you be more specific about what is not working for you?
Certainly, you do not have to manually format JSON in the general case.
tim_beynart wrote:It's been quite some time since I messed with it, but IIRC, my request body was blank if I tried to use an AA converted to JSON using formatJSON. The case is not an issue, I wrote that example off the top of my head. I can try to dig up an example to demonstrate.