How can I get Custom Payload as string - python

I have a Dialogflow CX agent that returns a Custom payload. My client is a Python application using the dialogflowcx_v3beta1 SDK to call DetectIntent. The application needs to forward the custom payload in JSON format to another application, but I have been unable to find a way to convert the structured payload to JSON. There is no schema associated with the custom payload, which could be literally any valid JSON, and because it will simply be forwarded to another component, the application has no reason to interpret the content in any way.
Is there a way to serialize the custom payload to JSON?

Unless you're asking a Python question, the "CX Solution" could be to use the Fulfillment text instead of the Custom Payload feature, and include there the serialized JSON.

Related

How to Form-encode a dictionary in Python?

I am interacting with a REST API and need to explicitly form-encode the request body before sending.
The urllib module has a urlencode method, but I see no equivalent for form-encode, presumably as dictionaries are automatically encoded when using urllib to POST the request.
Is there a built in method that I can use to form-encode an object? For example:
params={'f':'json','type':'map'}
post_body = formencode(params)
Producing post_body:
f=json
type=map
In this instance I'm using WinHttpRequest through the win32com module as it can take care of authentication natively so I don't need to handle credentials in the script.

How to send JSON payload to RabbitMQ using the web plugin?

I have a RabbitMQ 3.4.2 instance with a web management plugin installed.
When I push to the message {'operationId': 194} to the queue using Python's kombu queue package, the message is read on the other end as a dictionary.
However, when I send the message using the web console:
I get the following error on the receiving end:
operation_id = payload['operationId']
TypeError: string indices must be integers
I have tried adding a content-type header and property, with no success.
Since the reader code is the same, it means that the web sender does not mark the sent message as a JSON / dictionary payload, and therefore it is read as a string on the other end.
Any idea how to mark a message as a JSON message using the RabbitMQ web console?
I had to use content_type instead of content-type (an underscore instead of a hyphen).
This is a pretty questionable design decision, because the standard everybody knows is content-type.
You need to de-serialize the output.
import json
payload = json.loads(payload)
operation_id = payload['operationId']
In addition {'operationId': 194} is not valid JSON. Although it looks like you use double quotes in the screenshot, but make sure you replace the single quotes with double quotes.
Edit:
So you are correct, kombu should handle this. Looking at the code it's likely that the header is case-sensitive. Change the properties header from Content-Type to content-type.

POST parameters formatted as JSON

I am writing a web service using Django that will be consumed from a MS SharePoint workflow. In the SP workflow, I created a dictionary with 2 items (id:1, text:'foo'), and used this dictionary as the request content. However, instead of using the dictionary to format a traditional POST parameter list, it sends it as a JSON object in the body of the POST request, so instead of the expected:
id=1&text=foo
in the body of the request, there is this:
{"id":1,"text":"foo"}
which of course, in turn, does not get parsed correctly by Python/Django (I am not sure who exactly does the parsing). How can I either get it to parse JSON, or get SharePoint to send traditionally encoded POST parameters?
EDIT
I saw other posts that explain how to get the raw body and parse the JSON. I was looking for a solution that would either:
Make SharePoint send normal data, or
Get Django to respect the Content-type header that states the data is JSON
There is no need for any parsing at the framework level. The body of the post request is always available in request.body, so you can access it directly:
result = json.loads(request.body)
May be it will help you bit more to handle.
import json
import urlparse
json.dumps(urlparse.parse_qs("id=1&text=foo"))

Attach a custom view to spyne

I am using Spyne to provide SOAP interface to my backend, but a need one or more custom views accessible on certain URLS. For example to show some statistics in HTML.
How do i do that?
Please have a look at the multiple protocols example here: https://github.com/arskom/spyne/tree/master/examples/multiple_protocols
If you want to return raw data via Http, you must set your output protocol to HttpRpc, your output type in your service to ByteArray or String and ctx.transport.mime_type to whatever type you're returning

Send nested dict or list from a Python script to Rails app via HTTP

I need to send a nested list or dict from a Python script to Ruby on Rails 3 app via HTTP. Ideally, I want to be able to retrieve the object just by using Rails params[] in the controller.
I'm new to all this. Could you please tell me how to do this? I've read that formatting the nested list or dict to JSON might be the best way, but I don't know how to send JSON data via HTTP, let alone make the sent data be easily accessible using Rails params[].
It just depends on what kind of request you make. If you do a get request then a simple string formatted like so would work:
example.com/controller/action?param1=value&param2=other%20value
In Rails this would set params[:param1]='value' and params[:param2]='other value'`.
If you want to send json just send it as an HTTP post with the content type 'text/json', and make sure the url you post to ends in .json, like so:
example.com/controller/action.json
That will set the params hash with the same keys and values as your json.

Categories

Resources