The request for our service looks something like this:
GET http://[SERVICE]/Node:[id].Build?format=mime1,mime2,...,mimeN&template-id=[templateid]
Accept: multipart/mixed
Content-Type: application/json
body: json document
I am attempting to use ApacheBench to test benchmark this. Here is the call I am using:
ab -n 10 -c 2 -T 'application/json' -H 'Accept: multipart/mixed' 'http://phx5qa01c-02b0.stratus.phx.qa.ebay.com/.Build?format=text/html,text/plain&template-id=29b1468f-c8c3-db23-2f6f-74e112795540'
This call goes through, and results in an error since the expected json data is not there.Is there a way in ab to supply the necessary json along with this request. I see there are -p and -u commands to specify an input file, but those are for puts and posts.
I realize that this answer is six years late, but I think it is worth posting since I was banging my head against a wall on a very similar issue to this, in which I was trying to load test a URL which returned only JSON data, and my solution might help other readers encountering this issue. My issue was I kept specifying the -H option when I didn't need to. That kept making the server to send back an HTTP 406 response code (Not Acceptable) to my AB request. During most of my my troubleshooting, I had also kept -T 'application/json' in the AB request as well when I didn't need it. That is only used in conjunction with PUTs or POSTs (when using the -p switch). So I removed -H, and -T, and it worked. All that said, I see those two issues here. We need to be mindful that AB uses the GET method by default.
You are constraining AB by appending extra custom headers to the
request, by using the -H option: -H 'Accept: multipart/mixed', which
might make your target server think it's an invalid request and stop the
sequence right then and there. Just don't use -H unless you have a
really good reason why.
You are using the -T option: -T 'application/json' which
only works when you specify that in conjunction with -p and you
don't have -p anywhere in your command, which you don't want to use anyway since you are sending a GET and not a PUT or a POST.
So to fix this, simply remove both the -T and -H options, and it should work. Reminder to other readers: If on Windows, enclose the URL in double-quotes whenever it contains special characters like a "&", or a "?", as in this case.
ab -n 10 -c 2 'http://phx5qa01c-02b0.stratus.phx.qa.ebay.com/.Build?format=text/html,text/plain&template-id=29b1468f-c8c3-db23-2f6f-74e112795540'
Related
My command is:
curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d {"title":"test","content":"Content","status":"draft"}
The response I get:
{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}
Thanks!
From my earlier comments, I have the feeling that this error is related to the -d possibly needing to have quotes around the data text.
So in your case, it might be worth trying to put single quotes around the -d data text because you are currently using double quotes in the json text.
Thus, your curl command might look like this:
curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d '{"title":"test","content":"Content","status":"draft"}'
This is based on how the curl docs appear to show that quotes should be used with -d related data text based on the example under the "POST (HTTP)" section of the curl documentation here. I think this is what I have done in the past, as well, but I don't have a specific personal example on me at the moment.
Update
For what it's worth, when I tried running this curl type of command on my own, I got the following error when I didn't use single quotes around the json text:
{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}curl: (3) Port number ended with 'C' curl: (3) Port number ended with 'd'
When I ran the command with single quotes around the json data, like this: -d '{"title":"test","content":"Content","status":"draft"}', the command worked and a draft was added in WordPress. I also received back a json response that looks related to the new draft post.
Another thing to check might be the credentials used with the --user part. I ended up having to use an "Application Password" that I generated for a user in my WordPress account. This is different than my user's login password. If you haven't already tried using an "Application Password" in WordPress for this part, it might be worth trying to see if that fixes your issue. I found this when I:
Logged into my WordPress website
Clicked on the Users menu item
Clicked on a user
Scrolled down to the bottom of the user info page
Clicked on the Add New Application Password button
Ok, this was a windows specific problem, I guess most people out there are running unix/linux clients so this doesn't apply to them. I found the solution here: https://stackoverflow.com/a/7173011/15161479
The issue is with curl on windows, the quotes need to be escaped. I also did a couple other things like installing the "Application Passwords" plugin.
This is what my command looks like now
curl --user "user:application password" http://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" --data "{"""title""":"""test""","""content""":"""Content""","""status""":"""draft"""}"
Hope this helps some other people out there!
In slack you can script slackbot to post messages to a channel like this:
curl --data "$msg" $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random'
Now i'd like to mention a username as the first part of the message like msg="#joernhees hello self".
The problem with this is that if the --data argument of curl starts with an # sign it will interpret the string after the # as filename and post its content. Is there a way to make curl ignore the # sign and to send a literal # as the first char of a post request?
If you are on a new version of cURL you can also use the --data-raw option:
http://curl.haxx.se/docs/manpage.html#--data-raw
A word of warning is that looking my laptop it appears Yosemite ships with an older version of cURL.
In general if you're creating tools to post to Slack I'd recommend using an HTTP library in your script rather than calling out to a shell and invoking the curl command.
Actually i just found out i can do this (not sure it's the best option though):
curl --data '#-' $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random' <<< "$msg"
The trick is to tell curl to read from stdin #- and then pass the message in via that.
I am trying to send a DELETE request with a url parameter using CURL. I am doing:
curl -H application/x-www-form-urlencoded -X DELETE http://localhost:5000/locations` -d 'id=3'
However, the server is not seeing the parameter id = 3. I tried using some GUI application and when I pass the url as: http://localhost:5000/locations?id=3, it works. I really would rather use CURL rather than this GUI application. Can anyone please point out what I'm doing wrong?
The application/x-www-form-urlencoded Content-type header is not required (well, kinda depends). Unless the request handler expects parameters coming from the form body. Try it out:
curl -X DELETE "http://localhost:5000/locations?id=3"
or
curl -X GET "http://localhost:5000/locations?id=3"
#Felipsmartins is correct.
It is worth mentioning that it is because you cannot really use the -d/--data option if this is not a POST request. But this is still possible if you use the -G option.
Which means you can do this:
curl -X DELETE -G 'http://localhost:5000/locations' -d 'id=3'
Here it is a bit silly but when you are on the command line and you have a lot of parameters, it is a lot tidier.
I am saying this because cURL commands are usually quite long, so it is worth making it on more than one line escaping the line breaks.
curl -X DELETE -G \
'http://localhost:5000/locations' \
-d id=3 \
-d name=Mario \
-d surname=Bros
This is obviously a lot more comfortable if you use zsh. I mean when you need to re-edit the previous command because zsh lets you go line by line. (just saying)
Using REST Server 6.x-2.0-beta3, I'm trying to understand how to post to user.save.
curl -d 'XX' -v http://localhost/services/rest/service_user/save
I've tried to replace XX with:
account{'name':'myname','pass':'mypassword','mail':'my#email.org'}
account = {'name':'myname','pass':'mypassword','mail':'my#email.org'}
account="name=myname,pass=mypassword,mail=myemail.org"
account=name=myname,pass=mypassword,mail=myemail.org
account=myname,mypassword,myemail.org
But none of these seems to be right and finding any documention regarding this is next to impossible.
I've also tried the following:
curl -H "Content-Type: application/json" -d 'account={"name":"myname","pass":"mypassword","email":"123"}' -v http://localhost/services/rest/service_user/save
The error I get in this case is:
HTTP/1.0 406 Not Acceptable: Missing required argument account
Hi I also just started working with this module and wondering how to create content using JSON.
Just been able to create a simple node using this:
Post URL: http://path-to-site/services/rest/node
Request Header: Content-Type: application/json
Request Body: {"type":"story","title":"REST Test","body":"REST using JSON"}
I think you're using the wrong URL
I figured it out:
curl -H “application/x-www-form-urlencoded” -d "sessid=xxxx" -d "account[name]=MyName&account[pass]=mypass&account[mail]=myemail#gmail.com&account[conf_mail]=myemail#gmail.com" -v http://path-to-site/services/rest/service_user/save
You only have to add -d "sessid=xxxx" if you have configured Services to require a session. Make sure in that case to replace xxxx with your actual session id (from system.connect).
I've tried the following to send a line break with curl, but \n is not interpreted by curl.
curl -X PUT -d "my message\n" http://localhost:8000/hello
How can I send a line break with curl?
Sometimes you want to provide the data to be sent verbatim.
The --data-binary option does that.
Your shell is passing \ followed by n rather than a newline to curl rather than "my message\n". Bash has support for another string syntax that supports escape sequences like \n and \t. To use it, start the string with $' and end the string with ':
curl -X PUT -d $'my message\n' http://localhost:8000/hello
See ANSI-C Quoting in the Bash Reference Manual
There's a much easier way!
curl -X PUT -d $'my message\n' http://localhost:8000/hello
This will use ANSI-C Quoting to insert the newline character.
No piping, no data files. See also Sending Newlines with cURL.
The solution for someone who doesn't want to use files, and doesn't want to resort to shell escaping magic is:
curl -X POST --data-binary #- http://url.com <<EOF
line one
line two
EOF
But this is literal newlines in the post data payload, and not in form fields.
Had similar issue. While uploading a CSV file from Mac to cloud storage, new lines were being removed. After downloading it, the entire file looked like a single line. I tried adding different EOL characters \n \r \r\n with no success. Using --data-binary instead of -d solved the issue.
Btw this issue occurred only from Mac. -d worked just fine while making the call from CentOS machine. This very much looks like due to Mac's newline character. But don't feel like debugging any more.
Thanks a lot for your help.
curl -X PUT -d #filename.csv https://cloudstorage -H "content-type: text/csv"
vs
curl -X PUT --data-binary #filename.csv https://cloudstorage -H "content-type: text/csv"
(I ended up here with a slightly different question, so I'm just going to post my answer because it might help future explorers)
My solution applies to people who are sending form-style data, i.e. key/value pairs in a query string. Use the encoded line break, which is %0A, just like how an encoded space is %20. You can use http://meyerweb.com/eric/tools/dencoder/ to convert other symbols.
So if you want to set the key message to the value:
line one
another
you would send
curl --data "message=line%20one%0Aanother" http://localhost:8000/hello
A very easy way, just Shift-Enter in the console for the break. Very readable typing it in too.
curl -d "line1
line2" http-echo.com
Server gets this: line1\nline2
Do this to remove the line break:
curl -d "line1 \
line2" http-echo.com
Server gets this: line1 line2
Not an answer to your question, but I would work around it by creating a temporary file containing the message and line break, and give curl that file to work on:
curl -X PUT -d #message.txt http://localhost:8000/hello
From the manual:
If you start the data with the letter #, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data #foobar.
I was using Sendgrid with this code (copied below) originally found here https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html
\n\n worked in Gmail, but \n was ignored. I tried to double the escape and other suggestions. I also tried \r\n and that did not work in Gmail either. Note: I didn't bother to test other email clients, maybe it was a Gmail-specific problem.
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "your.email#example.com"}]}],"from": {"email": "example#example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'
Eventually I gave up looking for a solution and switched the text/plain to text/html and just used <br /> tags.
Someone suggested that Sendgrid converts plaintext to HTML if you have a tracking pixel enabled, which makes sense. Maybe the newlines were destroyed in the plaintext-to-html conversion process. I assume the client wants a tracking pixel, so decided to switch to HTML.