Grape POST request receiving JSON - ruby-on-rails-4

I am having problem with grape.
I have tried to look into their docs and google around.
I could not find any solution or sample regarding this.
Let say I am sending this kind of format to the POST request of grape:
{
"preferences": {
"play": {
"weekdays": "5",
"weekend": "8"
},
"grow": {
"weekdays": "4",
"weekend": "8"
}
}
}
Questions:
How do I setup the Grape params to receive this post?
I have tried something like this:
params do
optional :preferences, type: Hash do
optional :play do
optional :weekdays
optional :weekend
end
optional :grow, type: Hash do
optional :weekdays
optional :weekend
end
end
end
I am using postman to do the POST on my chrome. My question is, How do I set the Hash kind of params ? There are 3 options on postman which are form-data, form-urlencoded, and raw. I have tried with form-data and raw (json) it does not work for the raw json, it gave me an error saying that
The requested content-type 'text/plain' is not supported
Any idea how do I fix these problems?
Thank you very much

Answering question 1: If all parameters are optional then you don't even need the params block. Get rid of it and, for example, use params[:preferences][:play][:weekdays] to access the weekdays attribute. Just use the same idea to access other values.
Answering question 2: On Postman, use RAW but don't forget to set the header Content-Type to application/json. I've written the code below to play around with this example.
require 'grape'
class API < Grape::API
version 'v1', :using => :header, :vendor => 'alienlabz', :format => :json
format :json
resource :preferences do
post do
puts params[:preferences][:play][:weekdays]
end
end
end
run API

Related

PowerBI fetch data with GET request with body

I am trying to query an API where I have to pass username and password in the body. I have researched multiple methods but have not found any method which seems reasonable.
My query looks something like this:
URL:
localhost:8080/v1/project
HEADERS:
Content-Type: application/json
BODY:
{
"email": "test#test.com",
"password": "123456"
}
In the docs I have found WebActions.Request, which is aparently disabled in most contexts (https://learn.microsoft.com/en-us/powerquery-m/webaction-request).
Web.Contents only allows a body in POST requests (https://learn.microsoft.com/en-us/powerquery-m/web-contents).
I'd rather not write a Python script to do it as it requires a Python installation.
I tried this possible solution, but there was no success, as it would still perform a POST request: https://community.powerbi.com/t5/Power-Query/How-to-send-a-GET-method-with-content-data-with-M/m-p/943033/highlight/true#M32541
What reasonable way is there to achieve my goal to do a GET request with a body containing the credentials?

Flask cannot parse JSON in Tabulator AJAX request (using advanced configuration)

I regularly use Tabulator's setData() method. I usually set parameters in the URL args, and have no problems with it. But I now have a complex use case that will be easier to solve if I can put a JSON payload into the request.
I've followed the Tabulator documentation for an advanced configuration.
I've made a series of attempts (putting the JSON in various places, using quotes/double quotes in the JSON, etc) at trying to work out the problem. The Flask server always returns this error:
Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)
What makes me suspect the problem is with Tabulator, not Flask, is because I printed request.__dict__ and couldn't find the JSON in the request. (I.e. that seems to the reason for the error.)
The below example, which triggers the same error, is taken from the Fetch documentation (Tabulator uses the Fetch API).
Is there anything wrong with the below or should I be looking harder at Flask?
const data = { username: 'example' };
var ajaxURL = "/data/results";
var ajaxConfig = {
method:"POST",
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf_token,
},
body: JSON.stringify(data)
};
ResultsTable.setData( ajaxURL, {}, ajaxConfig);
Notes:
I'm using the latest version of Tabulator (4.9).
ResultsTable is set elsewhere in the code and is successfully loading default data when the page loads. The use case kicks in when the user sets their own parameters for the data.
The CSRF token, which is set elsewhere in the code, is there because Flask requires it.
The reason that is failing is that Tabulator will build out its own request body when it builds a request and that will override your config.
In your usage case, you will need to override the build in ajax request promise and add your own function that makes the ajax request and then resolves the data.
You can do this using the ajaxRequestFunc.
Checkout the Ajax Request Documentation for full details

ORDS RESTful oracle-apex - A formulate JSON response (POST method)

I'm new in oracle-apex and with ORDS.
My problame is:
when I was tried to bulid a RESTful web service on oracle apex, from
a procedure, and used:
l_response := apex_web_service.make_rest_request(
p_url =>l_rest_url,
p_http_method => 'POST',
p_body => l_request_body
);
from make a request.
On ORDS Handler Definition, I defined:
declare
l_response_body clob;
begin
l_response_body := '{"conf_code":"'||sys_guid()|| '","status":"APPROVED"}';
INSERT INTO JSON_TIMECARD_APPROVAL (id, json_data)
VALUES (sys_guid(),l_response_body);
:xxResponse :=l_response_body;
When xxResponse was defined an a OUT prameter
and scource type: RESPONSE.
I'm intrested to get the response in JSON format.
When i tried to call my web service(my web api https://apex.oracle.com/pls/apex/eliranhaa/timecards/approval/)the.
Parameter IN:
p_body =
{"timecard": [
{
"timecard_id":214804582301489177025033231688226094978
,"employee_id":214804582301403343291840593016821956482
,"week_of":"2020-18-03T00:00:00Z"
}
]
}
The Outcome:
{"DATA":"{\"conf_code\":\"A3695DE33828CA50E0530C4072644591\",\"status\":\"APPROVED\"}"}
I don't know why the outcome return as string instead of Json format (Respone return as CLOB TYPE).
I Have a reference from web api
https://apex.oracle.com/pls/apex/timecards/timecard/approval/
By the way this outcome is what i whant for my web service.
Tnx
Eliran
The first thing that stands out is that you're calling sys_guid twice. Don't you want to return the same value that you inserted?
I don't know why the outcome return as string instead of Json format
What you're seeing is your JSON data being escaped improperly because you're trying to put a single JSON value as your output. Try breaking it up instead.
For example, I created a handler that looks like this:
And here's the response, which is what I think you're looking for (see line 12):

How to do a POST and GET in VBscript?

I am new to VBscript and am looking for some help to do a POST to an API and pass it a JSON string containing id, password and a scope, then get an answer and parse it. Here is the call I need to make:
POST https://integrations.ezyvet.com/call/getAccessToken { "partner_id": "id8888", "client_id": "id12345", "client_secret": "secret12345", "grant_type": "client_credentials", "scope": “read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem" }
Here is my code:
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("c:\temp\JSONoutput.txt", True)
set json = CreateObject("Chilkat_9_5_0.JsonObject")
jsonStr = "{""partner_id"": ""id8888"", ""client_id"": ""id12345"", ""client_secret"": ""secret12345"", ""grant_type"": ""client_credentials"", ""scope"": ""read-diagnosticresult,read-diagnosticresultitem, read-diagnosticrequest,write-diagnosticresult,write-diagnosticresultitem""}"
success = json.Load(jsonStr)
If (success <> 1) Then
outFile.WriteLine(json.LastErrorText)
WScript.Quit
End If
set http = CreateObject("Chilkat_9_5_0.Http")
I need to make my POST here and get a response and am not sure how. Please help.
Thanks a million.
Hi and welcome to stack overflow!
You have tagged chilkat in your question, however you haven addressed it anywhere in the body or the tittle of it, so I was not sure if answer pointing this or not, so I will try to make a bit of both.
without chilkat
You can do this in pure vbs by using ajax, the short answer would be
Dim request
Set request = CreateObject("MSXML2.XMLHTTP")
request.open "GET", "http://www.example.com", False '(1)
request.send infoToSend '(2)
'(3)
Here you set eather "POST" or "GET"
infoToSend contains the information data, formatted as "key=value&key2..."
request.responseText here contains the servers answer as text, parse it as json if you need
You can find information here.
with chilkat
If you still want to use chilkat the main documentation of the http object is here, here is everything you need. If you need an example tho here I've found two:
making a request: https://www.example-code.com/vbscript/http_xmlHttpRequestVerbs.asp
sending a json: https://www.example-code.com/vbscript/http_put_json.asp
I wont paste it here because its too long but the core part of your interest is that you is:
set request = CreateObject("Chilkat_9_5_0.HttpRequest") '(1)
request.HttpVerb = "PUT" '(2)
success = request.LoadBodyFromString(xmlStr,"utf-8") '(3)
Set response = http.SynchronousRequest(endpointDomain,endpointPort,endpointSsl,request)' (4)
you have to create a httpRequest
you set here eather get or post
3.you load here your content, it is your json or what you will send but formatted appropiately
here you have response contain a HttpResponse object with the result
documentation on the HttpResponse, and the HttpRequest

JSONAPI strong params with Rails and Ember

I'm using Ember with ember-data and a rails api. I had a createRecord() and save() for the record that was working fine. The payload in the network tab for the post request to create the record in rails looks like: {data: {attributes: { foo: 'bar' } }.
In the rails controller, I have strong params like so: params.require(:data).require(:attributes).permit(:foo), which was working fine for a little while. Now when I send the request rails says that param is missing or the value is empty: data. If I look in the network tab in the browser the payload for the request still looks the same as stated above. If I puts params it only shows {"controller": "api/v1/answers", "action": "create"} and isn't showing the data payload at all.
Is there any reason why rails isn't picking up on the right params from ember now? I did try to add an association to the model that I'm trying to create, which is when it started failing. However, I rolled back to when it was working, but it's not working anymore.
I fixed this by going into the config/initializers/mime_types.rb file in the rails api and changing the file to look like:
api_mime_type = %W(
application/vnd.api+json
text/x-json
application/json
)
Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_type