Your Digital Media Has Never Looked So Good

 
dan_shneider
Topic Author
Posts: 19
Joined: Sun Jan 01, 2017 11:11 pm

Add params to POST request body

Mon Jan 02, 2017 9:47 am

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,
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Add params to POST request body

Mon Jan 02, 2017 11:56 am

save to tmp:/ and then roUrlRequest.postFromFile() ?
 
dan_shneider
Topic Author
Posts: 19
Joined: Sun Jan 01, 2017 11:11 pm

Re: Add params to POST request body

Mon Jan 02, 2017 12:01 pm

Thanks, but in this case, can you please explain how do I add header params and body params (json) to a request/file?
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Add params to POST request body

Mon Jan 02, 2017 12:19 pm

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?

regarding headers and cookies - that's covered in https://sdkdocs.roku.com/display/sdkdoc/ifHttpAgent
re formatting body - no, can't tell you off top of my head - but doing inspecting http in your browser (or Fiddler or packet sniffer) should put you on the right track
 
dan_shneider
Topic Author
Posts: 19
Joined: Sun Jan 01, 2017 11:11 pm

Re: Add params to POST request body

Mon Jan 02, 2017 12:29 pm

Thanks a lot
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Add params to POST request body

Mon Jan 02, 2017 2:49 pm

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).
https://github.com/belltown/
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Add params to POST request body

Mon Jan 02, 2017 3:13 pm

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).
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Add params to POST request body

Mon Jan 02, 2017 5:46 pm

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).

Correct, PostFromString should work too.
https://github.com/belltown/
 
dan_shneider
Topic Author
Posts: 19
Joined: Sun Jan 01, 2017 11:11 pm

Re: Add params to POST request body

Tue Jan 03, 2017 7:30 am

Thank you all for your replies.
The thing is, I do need Async POST (I am using the data retrieved on the response).
My server expects several values in a json format on the request body.
I am having problems doing so.
I am creating a json string but on the request body is shows as HTML Form URL Encoded: application/x-www-form-urlencoded
This needs to be in format of application/json

stringForRequest = FormatJson(MY_JSON_STRING,1)...
request.AsyncPostFromString(stringForRequest)...

Any tips?
 
dan_shneider
Topic Author
Posts: 19
Joined: Sun Jan 01, 2017 11:11 pm

Re: Add params to POST request body

Tue Jan 03, 2017 9:01 am

Solved:
mRequest.AddHeader("Content-Type", "application/json")
mRequest.AddHeader("Accept", "application/json")
 
Anish
Posts: 3
Joined: Sat Feb 18, 2017 2:48 am

Re: Add params to POST request body

Fri Apr 28, 2017 12:26 am

could you show the code you wrote for this issue to resolve......because i have been through the same problem...thanks in advance...
 
tim_beynart
Posts: 227
Joined: Wed Jul 15, 2015 8:30 am

Re: Add params to POST request body

Fri Apr 28, 2017 4:12 am

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"
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)
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

Re: Add params to POST request body

Fri Apr 28, 2017 1:03 pm

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.


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.

The only problem I can guess that you are running into is with respect to associative array key case preservation,
which might be an issue if you are assigning fields using a.Key=Value instead of a["Key"]=Value or a.AddReplace("Key", Value).

Value1="some value"
Value2="some other value"
a={}
a["Key1"]=Value1
a["Key2"]=Value2
? FormatJSON(a)
'==>
{"Key1":"some value","Key2":"some other value"}
 
tim_beynart
Posts: 227
Joined: Wed Jul 15, 2015 8:30 am

Re: Add params to POST request body

Mon May 01, 2017 11:49 am

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.

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.
 
User avatar
RokuKC
Posts: 315
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

Re: Add params to POST request body

Wed May 03, 2017 6:04 pm

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.


As another attempt at psychic debugging, I'm guessing that your call to FormatJSON was failing and returning an empty string.  
This doesn't have anything to do with the roUrlTransfer or POST part of your code, but could happen if you had non-POD (plain old data) values in the AA, for example, or something that was not directly supported by JSON.
I would expect that you would see an error message printed in the app debug console in that case.

https://sdkdocs.roku.com/display/sdkdoc ... 0asInteger)asString

Hope that helps.   :)

Who is online

Users browsing this forum: No registered users and 7 guests