How to post a json for a particular parameter using CURL - json

I try to do a POST request which contains number of parameters. One parameetr require a JSON file. I tried several options but i face issue with json. The parameter which requires json is 'swagger'..
Here is the curl request I try.[1] But looks like this is not accepted by server. Im getting following error;
"null is not supported"}curl: (6) Could not resolve host: swaggerimpl.json
How can i post the JSON using curl for a particular parameter?
[1]
curl -X POST -b cookies $SERVER/publisher/site/blocks/item-design/ajax/add.jag -d "action=implement&name=YoutubeFeeds&visibility=public&version=1.0.0&provider=admin&endpoint_type=http&implementation_methods=http&wsdl=&wadl=&endpointType=nonsecured&production_endpoints=http://gdata.youtube.com/feeds/api/standardfeeds&implementation_methods=endpoint" -H 'Content-Type: application/json' -d 'swagger=' #swaggerimpl.json
Edit :
Curl Command
curl -X POST -b cookies $SERVER/publisher/site/blocks/item-design/ajax/add.jag -d "action=implement&name=YoutubeFeeds&visibility=public&version=1.0.0&provider=admin&endpoint_type=http&implementation_methods=http&wsdl=&wadl=&endpointType=nonsecured&production_endpoints=http://gdata.youtube.com/feeds/api/standardfeeds&implementation_methods=endpoint" -d #swagger_impl.json -d #endpointconfig_impl.json;
Error;
at java.lang.Thread.run(Thread.java:695)
Caused by: java.lang.ClassCastException: org.mozilla.javascript.NativeJavaArray cannot be cast to java.lang.String
at
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
The suspect json file
swagger={
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
"authorizations": {
"oauth2": {
"scopes": [],
"type": "oauth2"
}
},
.........
}
The cast code:
public static boolean jsFunction_updateAPIImplementation(Context cx, Scriptable thisObj,
Object[] args, Function funObj) throws Exception, ScriptException {
boolean success = false;
if (args==null||args.length == 0) {
handleException("Invalid number of input parameters.");
}
NativeObject apiData = (NativeObject) args[0]; //This cause issue

Parameter that you are adding at the end should not contain the space. But if you remove this space then '#swagger.json' will be added as a test (not the file content). If you want to pass JSON as a parameter then you can add to the file parameter name like:
swagger={..}
It looks like workaround but curl will merge every -d parameter into the request parameters and it doesn't allow unquoted spaces.

Related

Always getting "Not found" message when using mapbox datasets api

When making a request to the MapBox API to create a new dataset we keep getting a response 404 {
"name": "foo",
"description": "bar"
}.
We are sending the code:
curl -X POST “https://api.mapbox.com/datasets/v1/username?access_token=TOKEN” \
-d #mapboxtest.json \
--header “Content-Type:application/json
Problem Solved!!!
It's just a wierd message. My token key is not allowed to change datasets, that was the problem. It was the default public key!

Failed to read body as a JSON Object on curl command

I am testing out push notifications for iOS Development and am currently using Pusher. When I run this command in terminal
curl -H "Content-Type:application/json" -H "Authorization: Bearer 7FF3763C208E26F2198CE44624E9486B1EC8B02A489DCF2F476FD511763887AE" -X POST 'https://d3ca6e4b-3222-49c9-8028-f9c2d28b3ad7.pushnotifications.pusher.com/publish_api/v1/instances/d3ca6e4b-3222-49c9-8028-f9c2d28b3ad7/publishes' -d#publish-body.json
It gives me this warning
Warning: Couldn't read data from file "publish-body.json", this makes an empty
Warning: POST.
{"error":"Bad Request","description":"Failed to read body as a JSON
object"}
Here is my JSON file
{
"interests": [
"Reminder"
],
"apns": {
"aps": {
"alert": {
"title": "Hello",
"body": "Hello, world!"
}
}
}
}
I tried going to the Pusher API and all it told me is that the error is due to a bad request. Any ideas on how to fix this? This app is a gift for my girlfriend and I would love to have this working!

Curl - POST request using JSON with query parameter

I have a URL https://mylink/apply?data= for a server which accepts the data variable as valid JSON of following properties:-
name: String with your name (required)
email: String with your email address (required)
urls: Array of strings with links
comment: String with any comment/request you might have (optional)
My JSON file "data.json" is the following:
{
"data":
{
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}
}
When I use the following CURL command to send a POST request, I get error code 400 Bad Request with a message from the server Error: no data passed. indicating something wrong with my JSON format or command. Not sure what I am doing wrong here
curl -v -i -X POST -d #data.json https://mylink/apply?data= -H "Accept: application/json" -H "Content-Type: application/json" -k
I am using Windows 7 if that matters
The API is pretty weird. But if you have to read JSON file and POST its content to server as URL parameter, you can cat the file, encode it and send it in curl. Here is an example:
curl -G -X POST https://requestb.in/1n88lah1 --data-urlencode data="$(cat input.json)"
The input.json would look like:
{
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}
Using Postman I was able to send this POST request with the following complete URL. Got a 200 OK response
https://mylink/apply?data={
"name": "Rogers Bell",
"email": "xxxx#gmail.com",
"urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
"comment":"none"
}

Can't set app-server to default values

I'm trying to reset an app-server from an LDAP configuration to default values at the command line using REST but it appears to not be working. The external-security element does not exist on the JSON or the XML marklogic properties webpage located on the 8002 port if it is not set.
http://localhost:8002/manage/v2/servers/App-Services/properties?group\
-id=Default&format=json
I think this might be a bug. I've tried null, 0, \0, [], and many other forms of null in JSON. The XML form does not work either.
curl -X PUT --anyauth -u admin:$(cat pass) --header "Content-Type:application/json" \
-d '{"authentication":"digest", "internal-security":true,"default-user":"nobody", \
"external-security":"" }' \
http://localhost:8002/manage/v2/servers/App-Services/properties?group-id=Default
Returns:
{"errorResponse":{"statusCode":"500", "status":"Internal Server Error",
"messageCode":"XDMP-VALIDATEBADTYPE", "message":"XDMP-VALIDATEBADTYPE: (err:XQDY0027)
validate strict { $nsfix } -- Invalid node type: srvprop:external-security lexical value
\"\" invalid for expected type #srvprop:external-security at
/srvprop:http-server-properties/srvprop:external-security using schema
\"manage-server-properties.xsd\""}}

Bash script: Use string variable in curl JSON Post data

I want to send a json request and embedd a variable in the post data.
I did a little research and I came up with the single quotes around the variable.
#!/bin/bash
FILENAME="/media/file.avi"
curl -i -X POST -H "Content-Type: application/json" —d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"file":"'$FILENAME'"}}}' http://192.167.0.13/jsonrpc
Unfortunately I get some errors:
curl: (6) Couldn't resolve host '—d'
curl: (3) [globbing] nested braces not supported at pos 54
HTTP/1.1 200 OK
Content-Length: 76
Content-Type: application/json
Date: Wed, 29 Jan 2014 19:16:56 GMT
{"error":{"code":-32700,"message":"Parse error."},"id":null,"jsonrpc":"2.0"}
Appearently there are some problems with the braces and the http answer states, that the command could not be executed. What's wrong with my code here?
Thanks!
This is my curl version:
curl 7.30.0 (mips-unknown-linux-gnu) libcurl/7.30.0 OpenSSL/0.9.8y
Protocols: file ftp ftps http https imap imaps pop3 pop3s rtsp smtp smtps tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL
Update: use the simpler
request_body=$(cat <<EOF
{
"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"file": "$FILENAME"
}
}
}
EOF
)
rather than what I explain below. However, if it is an option, use jq to generate the JSON instead. This ensures that the value of $FILENAME is properly quoted.
request_body=$(jq -n --arg fname "$FILENAME" '
{
jsonrpc: "2.0",
method: "Player.Open",
params: {item: {file: $fname}}
}'
It would be simpler to define a variable with the contents of the request body first:
#!/bin/bash
header="Content-Type: application/json"
FILENAME="/media/file.avi"
request_body=$(< <(cat <<EOF
{
"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"file": "$FILENAME"
}
}
}
EOF
))
curl -i -X POST -H "$header" -d "$request_body" http://192.167.0.13/jsonrpc
This definition might require an explanation to understand, but note two big benefits:
You eliminate a level of quoting
You can easily format the text for readability.
First, you have a simple command substitution that reads from a file:
$( < ... ) # bash improvement over $( cat ... )
Instead of a file name, though, you specify a process substitution, in which the output of a command is used as if it were the body of a file.
The command in the process substitution is simply cat, which reads from a here document. It is the here document that contains your request body.
My suggestion:
#!/bin/bash
FILENAME="/media/file 2.avi"
curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"file":"'"$FILENAME"'"}}}' http://192.167.0.13/jsonrpc
The differences are hyphen in -d (instead of a dash) and double quotes around $FILENAME.
Here is another way to insert data from a file into a JSON property.
This solution is based on a really cool command called jq.
Below is an example which prepares request JSON data, used to create a CoreOS droplet on Digital Ocean:
# Load the cloud config to variable
user_data=$(cat config/cloud-config)
# Prepare the request data
request_data='{
"name": "server name",
"region": "fra1",
"size": "512mb",
"image": "coreos-stable",
"backups": false,
"ipv6": true,
"user_data": "---this content will be replaced---",
"ssh_keys": [1234, 2345]
}'
# Insert data from file into the user_data property
request_data=$(echo $request_data | jq ". + {user_data: \"$user_data\"}")