Ratpack/Groovy - read the body of a call - docker

I want to create a "mock" server using Ratpack/Groovy and we need to read the data-raw from a call
I haven't had a problem reading headers or parameters, but I need to read the body of a request
Examplle:
curl --location --POST request 'http://localhost:5050/authenticate' \
--header 'Content-type: application/json' \
--raw-data '{
"username": "testbase01#domain.com",
"password": "12345678"
}'
we need to parse and process the username and password fields
We have tried to read the request.body, or the body.text, but we have not been successful.
Basically the example found in the solution at https://stackoverflow.com/questions/23636248/parse-json-in-ratpack-groovy would be what we need, but it hasn't worked for us either
Our environment has the following dependencies
#Grab('io.ratpack:ratpack-groovy:1.6.1'),
#Grab('org.slf4j:slf4j-simple:1.7.22'),
#Grab("io.ratpack:ratpack-jackson:0.9.17")
and we are using https://github.com/Vad1mo/hello-world-rest as primary image
If you have a small example, to be able to adapt it to our needs

Related

Twilio - How to update Task attributes defined in IVR flow for an inbound call?

I am wondering how to update event.TaskAttributes.variable value of a current task. I thought it's straight forward from the example by Twilio but I am not seeing any change If I followed the same and try to set/update a value of a specific attribute (defined in IVR voice flow widget).
https://www.twilio.com/docs/taskrouter/api/task#action-update
For example, there is an attribute called language which is a gather input digits field in IVR flow and at some point in the execution (while the caller is in waiting queue), we would like to update it to a different value. I tried via postman but it does nothing. Any help is greatly appreciated.
https://taskrouter.twilio.com/v1/Workspaces/WSXXXXXXX/Tasks/WTXXXXXX
{
"attributes": {
"language": "6"
}
}
Thanx!
In Postman use url-encoded format. The request should be encoded. Use 'Attributes' in place of 'attributes'. I am attaching a sample cURL request, this might be helpful
curl -X POST \
https://taskrouter.twilio.com/v1/Workspaces/WS...../Tasks/WTXXXX...... \
-H 'Authorization: Basic XXXXXXXXXXXXX' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'Attributes=%7B%22test2%22%3A%201%7D'

Postman-generated cUrl and Swift codes result in status 415, but when running the same from postman itself I get 400 (which is correct)

I need to post a single key-value pair from a Swift iOS app using form-data.
The Postman example that was provided by the back-end developer works:
The Postman Body tab shows "SelectedCreditCard" under "KEY" and "34" under "VALUE"
The method is POST.
The Postman Headers tab shows "Content-Type" under KEY and "application/x-www-form-urlencoded" under VALUE, and an additional authorization token header.
When running from Postman I get status 400 with a JSON body that contains an "Invalid credit card" warning. This is the expected result.
When implementing this in Swift I received 415 (media not supported). I pasted the curl code generated by Postman to the Mac command line, just to see how it looks, and to my surprise I got the same result as in the iOS App.
The curl code:
curl --location --request POST 'https://stg.xxx.com/api/xxx/SavePaymentInfo' \
--header 'Authorization: Bearer xxxxxxxxxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--form 'SelectedCreditCard=34'
The curl result:
{"$type":"Microsoft.AspNetCore.Mvc.ProblemDetails,
Microsoft.AspNetCore.Mvc.Core",
"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title":"Unsupported Media Type",
"status":415,
"traceId":"0HLTFOL7EGBUA:00000007"}%
I have also tried the generated Objective C code provided by Postman and the generated Swift code, and in all cases the result is 415. The only case in which I get the correct result (400 with an invalid card warning) is when running the post directly from Postman itself. I wonder what causes this gap.
Found the solution: in curl, instead of "Content-Type", the header name should be "Accept" (no need to specify a Content-Type)

Is Avro Schema Registry compatibility lost when using arrays?

I'm using Confluent Avro Schema Registry with backward compatibility option. I upload this schema which is a record inside an array (the [ ]-brackets). This record only contains one string field.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname\", \"type\": \"string\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
Everything goes well and I get a success response back (200).
Now I want to sanity check this so I remove the string field and I add a integer field. This should definitely not be compatible, neither forward nor backwards.
curl -X POST -i -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "[{\"type\": \"record\", \"name\": \"TestRecord\", \"namespace\": \"com.company\", \"fields\": [{\"name\": \"testname2\", \"type\": \"int\"}]}]"}' http://localhost:8081/subjects/test99-value/versions
The problem is that this also returns a success even though this cannot possibly be correct. Am I missing something very basic here?
If I do this with a record that isn't inside an array I get the expected error from the schema registry.

cURL POST --data-binary vs --form

I have a simple question regarding to the usage of cURL. Didn't find much during my Google search or Man page to get a clear answer.
In here talks about using either --data vs --form on sending file/attachment. I'm curious to know what are the main difference and under what scenarios you would pick --data-binary VS --form ?
The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):
-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=#foovalue.txt" # the specified file is sent as an attachment
-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=#foovalue.txt"
-d "#entirebody.txt" # the specified file is used as the POST body
--data-binary "#binarybody.jpg"
The difference is explained in the HTML 4.01 Specification section on Forms:
application/x-www-form-urlencoded is the default content type.
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
That's exactly the main difference, type of data that's being sent to the server (application/x-www-form-urlencoded vs multipart/form-data)

How to update user model from api in rails?

I am new to rails and i want to produce json response for iPhone app from server. I have user model where they can get the current user information after login using the token
curl -H 'Content-Type: application/json' -H 'Accept:
application/json' -X GET htt://localhost:3000/profile.json -d
"{"user" : { "auth_token" : "asjyWYeyVHDJrg746h" }}"
this provides the user information, similarly how can the user be edited or updated by passing the values that are to be changed.Please help me.
Look at Grape. Also read about respond_to :json

Resources