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"
}
Related
I want to create a script with helping of this document but I don't know how to pass a groovy file instead of json and when I execute
curl -u admin:admin123 -X POST --header 'Content-Type: application/json' \ ─╯
http://<url>/service/rest/v1/script \
-d create_task.groovy
I got this error:
{ "id" : "*", "message" : "Could not process the input:
Unrecognized token 'create_task': was expecting (JSON String, Number,
Array, Object or token 'null', 'true' or 'false')" }%
Any help would be appreciated!
enter code hereI found the solution!
you have to first convert that groovy script to a JSON file and pass that JSON file to your curl or API(for creating script) that you are working with and we can do it with this python file:
import json
with open("[THE GROOVY FILE]", "r") as inputfile:
filedata = inputfile.read()
jsondata = {}
jsondata['name'] = 'testscript2'
jsondata['type'] = 'groovy'
jsondata['content'] = filedata
with open("[OUTPUT FILE]", "w") as outputfile:
outputfile.write(json.dumps(jsondata))
the output file is something like this:
{
"name": "name",
"type": "groovy",
"content": "..."
}
then use this output file which is a JSON file and pass this to that command and there you go!
the command for creat script with REST API is:
curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script" -d #{output}.json
the above command will create a script and you can execute it with:
curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script/{name}/run
I want to pass the request body, but it doesn’t work out for me, maybe I wrote it wrong
curl -H "Content-Type: application/json" -X POST "{"time": "2019-12-27T09:50:02.000+0000", "unread:" "true", "message": "hello","from":"Vadim"}" localhost:8080/api/save
You need to escape the json body or use single quote to wrap your json
curl -XPOST -H "Content-type: application/json" -d '{"time": "2019-12-27T09:50:02.000+0000", "unread:" "true", "message": "hello","from":"Vadim"}' 'localhost:8080/api/save'
Or on windows
curl -X POST -H "Content-type: application/json" -d "{'time': '2019-12-27T09:50:02.000+0000', 'unread': 'true', 'message': 'hello','from':'Vadim'}"
You can get the crul command from the postman
click on the code from drop down get the cURL which will give the cur command from that you can run
refrence link : https://learning.getpostman.com/docs/postman/sending-api-requests/generate-code-snippets/
If you have swagger enabled for you Spring Boot APP from that also you can able to see crul command
-X POST "{"time": "2019-12-27T09:50:02.000+0000", "unread:" "true", "message": "hello","from":"Vadim"}"
Your JSON is invalid. {"unread:" "true"} should be {"unread":"true"}.
After that you need to escape the JSON's double quotes:
-d "{\"time\":\"2019-12-27T09:50:02.000+0000\",\"unread\":\"true\",\"message\":\"hello\",\"from\":\"Vadim\"}"
Obviously I can't test your curl command, but I think this should work for you.
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!
How do I can send JSON object to solr collection usinc cUrl
I'm using Windows 10.
curl -X POST -H "Content-Type:application/json" "http://localhost:8983/solr/solr_sample/update/json/docs" --data-binary "{'id': '1','title':'Doc 1'}"
When I'm using this format I'm getting some kind of warning message:
curl -X POST -H 'Content-Type:application/json' 'http://localhost:8983/solr/sorl_sample/update/json/docs' --data-binary '{"id": "1","title":"Doc 1"}'
curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (3) [globbing] unmatched close brace/bracket in column 14
I resolved it using " " insted of ' '
When I send the request using the first url I'm getting this response:
{
"responseHeader":{
"status":0,
"QTime":112}}
But when I try to get some result by searching I can't see any object in docs[]
curl -X GET "http://localhost:8983/solr/solr_sample/select?q=*:*"
Result:
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*"}},
"response":{"numFound":0,"start":0,"docs":[]
}}
When I'm using Solr UI I can add JSON objects without any problems, also to see the result in terminal
You have to commit the update. Add commit true or commitWithin 1000 to the request, e. g.
curl -X POST -d '{"add":{"doc":{"id":"delete.me","title":"change.me"},"commitWithin":1000}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update
Does also work:
curl -X POST -d '{"add":{ "doc":{"id":"delete.me","title":"change.me"}}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update?commit=true
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\"}")