How can I post JSON data with curl and use it in RoR? - ios

I am building an iOS app and it needs to post to a RoR API.
To test the POST, I am running this command:
curl -d #/tmp/data localhost:3000/api/ratings
Where /tmp/data contains an array of hashes:
[ { "uid": "gilt_162929239", "rate": 1 } ]
In Ruby, params looks like this:
[1] pry(#<Api::PromotionRatingsController>)> params
=> {" { \"uid\": \"good_162929239\", \"rate\": 1 } "=>nil,
"format"=>"json",
"action"=>"create",
"controller"=>"api/ratings"}
How do I get Rails to create the array of hashes for me to iterate over with each?
Edit: I see I was missing headers.
Adding to the curl command line:
--header "Accept: application/json" --header "Content-type: application/json"
Now, here are my params:
[1] pry(#<Api::PromotionRatingsController>)> params
=> {"_json"=>[{"uid"=>"good_162929239", "rate"=>1}],
"format"=>"json",
"action"=>"create",
"controller"=>"api/ratings",
"promotion_rating"=>{"_json"=>[{"uid"=>"good_162929239", "rate"=>1}]}}
Still not right.

According to this:
if you’ve turned on config.wrap_parameters in your initializer or
calling wrap_parameters in your controller, you can safely omit the
root element in the JSON/XML parameter. The parameters will be cloned
and wrapped in the key according to your controller’s name by default.
So, my guess is that you either need to include a root element, or set config.wrap_parameters.

Related

How to send a POST request with parameters in Insomnia?

I have the following POST method definded in OpenAPI:
post:
tags:
- Courses
description: Creates a new Course and add it to specified Program
parameters:
- name: Program
in: path
description: Specified Program to add the new course to
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Course'
In insomnia I can define the Course object, I want to add via the body/JSON tab, however how can I define the needed parameter? It doesn't work in the Query tab the same way it does for GET methods.
Do I manually set the path of the POST request with the parameter, or is there a build in way (or is it not possible at all)?
Here is the curl when trying to add the Program Parameter in the Query tab:
curl --request POST \
--url 'http://localhost:8080/Courses?Program=Testprogram' \
--header 'content-type: application/json' \
--data '{
"name": "TestCourse",
"type": "UE",
"etcs": 26,
"courseID": 909090
}'
I had the exact same issue and my problem was an internal redirect (nginx) from HTTP to HTTPs which changed the request type and made it impossible to maintain the body of the requests.
Oddly enough it worked with "Multipart Form".
So please make sure you provide your FULL URI in Insomnia, including the protocol to use for the request.
I found this issue by copying the request from the Insomnia GUI as a cURL command and pasting it into my terminal, which gave me an 301 Permanently moved. ;)

Cumulocity - Send Measurement/Alarm/Event using external ID via HTTP

I've been recently exploring Cumulocity and managed to use the external ID to send data (measurements/alarms/events) via MQTT. Its well documented and pretty straight forward.
But I cant find how to send data (measurement/alarm/event) using ExternalID instead of source.
For example, here is how POST of a measurement looks like if you know ClientID of device:
curl -X POST \
https://myTenant.cumulocity.com/measurement/measurements \
-H 'Accept: application/vnd.com.nsn.cumulocity.measurement+json' \
-H 'Authorization: Basic mytoken' \
-H 'Content-Type: application/json' \
-d '{
"c8y_TemperatureMeasurement": {
"T": {
"value": 25,
"unit": "C" }
},
"time":"2019-03-07T10:03:14.000+11:00",
"source": {
"id":"1234567" },
"type": "c8y_TemperatureMeasurement"
}'
Is there a way to replace that "source": {"id":"1234567" }, with external ID?
What would the request look like?
As of today, this is not possible:
Instead you have to first convert the externalID to the source id once (e.g. when the device is booted its done as first actions). Afterwards send all requests (e.g. POSTs to create measurements/alarms/events) using this retrieved sourceID.
This is also described in the Device SDK for HTTP here: https://cumulocity.com/guides/device-sdk/rest#step-1-check-if-the-device-is-already-registered .
Thanks for the good feedback on the documentation!

How to take data in google sheet script via POST request in JSON format?

This question is about receiving POST request from somewhere. I'm looking for a google sheet script function that can take and handle data from the POST request in JSON format. Could you suggest any example?
The POST request is here:
https://script.google.com/macros/s/BOdirjv45Dn6FHrx_4GUguuS6NJxnSEeviMHm3HerJl4UsDBnDgfFPO/
{
"p1": "writeTitle",
"p2": [[URL]],
"p3": [[PIC_A]],
"p4": [[PIC_B]],
"p5": [[TITLE]]
}
application/json
doPost() doesn't work:
doPost(e) {
var json = JSON.parse(e.postData.contents);
Logger.log(json);
}
You want to retrieve the value from the request body as an object.
You have already deployed Web Apps.
If my understanding of your situation is correct, how about this modification?
Post and retrieved object:
As a sample, I used the following curl command to POST to Web Apps.
curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec"
When above command is run, e of doPost(e) is as follows.
{
"parameter": {},
"contextPath": "",
"contentLength": 90,
"queryString": "",
"parameters": {},
"postData": {
"type": "application/json",
"length": 90,
"contents": "{\"p1\": \"writeTitle\",\"p2\": \"[[URL]]\",\"p3\": \"[[PIC_A]]\",\"p4\": \"[[PIC_B]]\",\"p5\": \"[[TITLE]]\"}",
"name": "postData"
}
}
The posted payload can be retrieved by e.postData. From above response, it is found that the value you want can be retrieved by e.postData.contents. By the way, when the query parameter and the payload are given like as follows,
curl -L \
-H 'Content-Type:application/json' \
-d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
"https://script.google.com/macros/s/#####/exec?key=value"
value can be retrieved by e.parameter or e.parameters. And the payload can be retrieved by e.postData.contents.
Modified script:
In this modified script, the result can be seen at the Stackdriver, and also the result is returned.
function doPost(e) {
var json = JSON.parse(e.postData.contents);
console.log(json);
return ContentService.createTextOutput(JSON.stringify(json));
}
Note:
When you modified your script of Web Apps, please redeploy it as new version. By this, the latest script is reflected to Web Apps. This is an important point.
Reference:
Web Apps
Stackdriver Logging
If this was not what you want, I'm sorry.

Marketo REST API: Associating a lead with a program

I've searched high and low and don't believe this is possible, but before giving up, thought I'd ask on here. Is there any way that anyone's found - roundabout or not - to create a lead and associate it with a program in Marketo via their REST API?
Thanks!
Why not using a smart campaign that automatically associate leads with the relevant program?
You can create some custom field in MLM (for example: "Associate with Program") and when you create a new lead with the API add some value in this field, i.e "associateWithProgram" : "program123".
Then, in MLM create a campaign that is being triggered whenever a new lead is created and the associateWithProgram field is not empty.
The flow will be to add the lead to the desired program.
curl -X POST \
"${baseUrl}rest/v1/leads.json?access_token=${AccessToken}" \
-H "Content-Type: application/json" \
-d '
{
"action":"createOrUpdate",
"lookupField":"email",
"input":[
{
"email":"lead#customer.com",
"firstName":"Sergejs",
"lastName":"test"
},
{
"email":"lead#company.com",
"firstName":"Sergejs",
"lastName":"test1"
},
{
"email":"lead#mail.net",
"firstName":"Sergejs",
"lastName":"test2"
}]
}
'
LeadId=$(curl -X GET "${baseUrl}rest/v1/leads.json?access_token=${AccessToken}&filterType=email&filterValues=${leademail}" | jq '.result | .[].id')
curl -X POST "${baseUrl}rest/v1/leads/programs/${NewProgramId}/status.json?access_token=${AccessToken}" \
-H "Content-Type: application/json" \
-d " {
\"status\": \"Registered\",
\"input\": [
{\"id\": ${LeadId} }
]
}
"

How to send Parameters to a map reduce query

I'm writing a mapreduce query in erlang for Riak and I want to pass parameters into Riak using the HTTP API through curl on an Ubuntu terminal. The input to the query is a 2i query but I want a parameter to allow further filtering. I thought options was the keyword since the python client is what I'll be using in production, but it's inconvenient for proofing my Erlang, and it's the keyword that's always used on my team.
This is what I'm trying:
curl -X POST http://riakhost:port/mapred -H 'Content-Type: application/json' -d '{
"inputs": {
"bucket":"mybucket",
"index":"field1_bin",
"key":"val3"
},
"options": "test",
"query": [
{"map": {"language": "erlang",
"module": "mapreduce",
"function":"map"
}},
]}'
On a three record set I am seeing:
["none", "none", "none"]
But I want:
["test", "test", "test"]
What is the format for arguments?
I developed a set of configurable utility functions for Riak mapreduce in Erlang. As I wanted to be able to specify sets of critera, I decided to allow the user to pass configuration in as a JSON document as this works well for all client types, although other text representations should also work. Examples of how these functions are used from curl are available in the README.markdown file.
You can pass an argument to each individual map or reduce phase function through the 'arg' parameter. Whatever you specify here will be passed on as the final parameter to the map or reduce phase, see the example below:
"query":[{"map":{"language":"erlang","module":"riak_mapreduce_utils",
"function":"map_link","keep":false,
"arg":"{\"bucket\":\"master\"}"}},

Resources