Ransack query on multiple fields with curl - ruby-on-rails

Using the Ransack Rubygem, I am trying to make a query against two conditions using a curl. This for searching ability being added to an API.
Query on a single field
curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_eq]=7610200237576'
This works
Processing by Api::V2::ProductsController#index as */*
Parameters: {"q"=>{"barcode_eq"=>"761063205021"}}
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."barcode" = '761063205021' LIMIT 50 OFFSET 0
Query on two fields
curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_eq]=7610200237576' -d 'q[barcode_eq]=7616800205113'
Ignores the first query
Started GET "/api/v2/products?q[barcode_eq]=7610200237576&q[barcode_eq]=7616800205113" for ::1 at 2017-01-10 15:34:28 +0100
Processing by Api::V2::ProductsController#index as */*
Parameters: {"q"=>{"barcode_eq"=>"7616800205113"}}
Product Load (0.6ms) SELECT "products".* FROM "products" WHERE "products"."barcode" = '7616800205113' LIMIT 50 OFFSET 0
What is the correct sentence to search (and / or) against multiple fields using curl or via ajax.

I would use the *_in matcher:
curl -X GET -G 'http://localhost:3000/api/v2/products' -d 'q[barcode_in][]=7610200237576' -d 'q[barcode_in][]=7616800205113'
Note the _in instead of the _eq and the empty brackets ([])

Related

How to POST with curl and not get HTTP error 422 'Unprocessable Entity'?

I am trying to send a POST request to an sign in endpoint. I keep getting HTTP error 422 'Unprocessable Entity'. How do I get past this error?
Here are some examples of commands I have tried:
curl -v -X POST -F 'user[email]=test#email.com' -F 'user[password]=password' https://example.com/users/sign_in
curl -v -H 'Accept: text/html,application/xhtml+xml,application/xml;' -H 'Content-Type: application/x-www-form-urlencoded' -d 'utf8=%E2%9C%93&authenticity_token=123456789123456789123456789123123123456456456789789789789VX4KgMBr6zgGjo123456789123456789njQ%3D%3D&user%5Bemail%5D=test%40sign_in.com&user%5Bpassword%5D=password&commit=Sign+in' https://example.com/users/sign_in
I want the request to send back a 200 or 301/302 response status, but instead I get this:
* upload completely sent off: 208 out of 208 bytes
< HTTP/1.1 422 Unprocessable Entity
< Server: nginx/1.12.2
< Date: Fri, 08 Feb 2019 12:08:07 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< X-Request-Id: 6ceb5baa-7740-448a-8105-6f67dd203fbb
< X-Runtime: 0.021676
Any help or advice is appreciated. Thanks
Update
I see now that the Rails log from such a request renders an InvalidAuthenticityToken Error
Started POST "/users/sign_in" for 127.0.0.1 at 2019-02-08 14:30:50 +0000
Processing by Users::SessionsController#create as */*
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
Any ideas on how to get past this? All I am seeking to do is ensure that routes which require authentication are returning 200/301/302 responses.
There are at least two ways to do this:
Option 1: get and use a token
One way to solve this is to actually capture and utilize the authenticity token that rails generates. To do this, we also need to capture and then send back cookies, to allow ourselves to utilize a session that is associated with the generated token. I was able to get this to work with something like the following incantation.
(Note: I'm assuming a scaffold has been created for "thing", creating a things_controller.rb, etc. Adapt this to your use case as needed.)
step 1: capture the token:
token=$(curl -s -c myjar https://example.com/things/new | \
sed -n 's/.*name="authenticity_token" value="\([^"]*\)".*/\1/p')
step 2: use it in the request:
cat myjar | \
curl -b - -s \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d '{"thing": {"field1":"Test","field2":"Testing"},
"authenticity_token":"'"$token"'"}' \
https://example.com/things | jq .
This works, without any changes to the application. When I run that, I see a pretty rendering of the JSON from a successful object creation as output.
(Ignore the | jq . if you just want the raw JSON data, perhaps to send it along to something else.)
However, there is another option. With a small tweak to the controller, one can achieve a bit easier curl usage (no cookiejar required, fewer parameters, only a single curl command, and no strange nested quotes), as follows:
Option 2: disable token checking:
Another way to get past this is to disable the validity_token checking for the create action in any controller(s) you wish to allow this sort of thing from. For example, given my example of a scaffold called "thing", one might update the app/controllers/things_controller.rb file to add the second line shown here:
class ThingsController < ApplicationController # EXISTING LINE
skip_before_action :verify_authenticity_token, only: [:create] # NEW LINE
before_action :set_img, only: [:show, :edit, :update, :destroy] # EXISTING LINE
# ... additional content elided ...
And then rails simply won't do that check, so one can do a one-step requset something like:
curl -s \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d '{"thing": {"field1":"Test","field2":"Testing"}}' \
http://example.com/things | jq .
Again, I see the jq-generated output, this time without having to capture cookies or a token or anything.

How to add values from 2 integer columns in influxdb

Inserted data as :
curl -i -XPOST 'http://localhost:8086/write?db=mathdb&precision=ms' --data-binary 'float_op_1,agentId=hell psatt=1.5,csatt=1.5'
Now if i run a select query with psatt + csatt I get
panic: runtime error: index out of range
This is a known regression bug due for a fix in the next release: https://github.com/influxdb/influxdb/issues/4046

Elasticsearch: reached maximum index count for current plan - status:500

Is there a Heroku cli command that will allow us to purge Elasticsearch indices?
This is the error that's returned which is causing our application to be non-functional:
reached maximum index count for current plan - status:500
Upgrading our current plan is not a feasible option due to being in stages at this time.
Please advise on how to cure this.
You should be able to simply delete the indices you want via a direct curl call. You first need to retrieve the URL of your ES cluster using the heroku config:get command and retrieving the appropriate variable:
If you've installed the SearchBox addon:
SB_URL=$(heroku config:get SEARCHBOX_URL)
curl -XDELETE http://<SB_URL>/your_index
If you've installed the Found addon:
FOUND_URL=$(heroku config:get FOUNDELASTICSEARCH_URL)
curl -XDELETE http://<FOUND_URL>/your_index
Last but not least if you've installed the Bonsai addon:
BONSAI_URL=$(heroku config:get BONSAI)
curl -XDELETE http://<BONSAI_URL>/your_index
To test this out:
# create one index => OK
> curl -XPOST http://<***_URL>/your_index_1
{"acknowledged":true}
# create second index => OK
> curl -XPOST http://<***_URL>/your_index_2
{"acknowledged":true}
# create n'th index allowed by your plan => NOT OK
> curl -XPOST http://<***_URL>/your_index_n
{"error":"ElasticsearchGenerationException[reached maximum index count for current plan]","status":500}
# delete first index => OK
> curl -XDELETE http://<***_URL>/your_index_1
{"acknowledged":true}
# create n'th index again => OK
> curl -XPOST http://<***_URL>/your_index_n
{"acknowledged":true}
Shortly after posting this, I found a non-cli solution.
log into Heroku
Select the "Resources" tab
Select "SearchBox Elasticsearch" (this is what I have installed, yours may be different)
Select Dashboard > Indices
Select the trash icon next to the index you want to delete

Using curl for lira API with a period in the fixVersion jql

I've tried various iterations of using either ", ' and ` to enclose a curl query to an instance of jira in order to get all issues for a particular fix Version.
curl -D- -u username:password -X POST -H "Content-Type: application/json" -d '{"jql":"project = PROJ AND fixVersion=Version-1.2.3"}' "https://thejirainstall.com/jira/rest/api/2/search"
However, using this and a couple of other change on fixVersion such as:
fixVersion="Version-1.2.3"
or
fixVersion=\"Version-1.2.3\"
or
fixVersion=Version-1\u002e2\u002e3
Add and remove quotes at will.
The ones that don't fail outright return:
{"errorMessages":["Error in the JQL Query: '\\.' is an illegal JQL escape sequence. The valid escape sequences are \\', \\\", \\t, \\n, \\r, \\\\, '\\ ' and \\uXXXX. (line 1, character 38)"],"errors":{}}
How do I either escape periods . or add another set of quotes?
Ok, so it turns out that Jira doesn't permit version names in jql syntax. The version id must be used instead.
And, in order to get the version id you must parse the result from https://thejirainstall.com/jira/rest/api/2/project/ON/versions?
This now means that I have to use a JSON parser anyway. So, now I'm using jq via homebrew install jq
My current solution is to write a bash script as below:
JIRA_FIXVERSION
fixVersionQuery='https://thejirainstall.com/jira/rest/api/2/project/ON/versions?';
myJSONResponse=`curl -u username:password -X GET -H "Content-Type: application/json" --insecure --silent $fixVersionQuery |jq '.[] | {id,name} | select(.name=="Version-1.2.3" | .["id"]'`;
echo $myJSONResponse;

Apache Bench and POST data

i am trying to use apache bench to load test a create action in my rails application but ab doesn't appear to be sending the POST data - though it does correctly submit a POST and not a GET request.
this is the command i run:
ab -n 1 -p post -v 4 "http://oz01.zappos.net/registrations"
and this is the contents of the post file:
authenticity_token=M18KXwSOuIVbDPZOVQy5h8aSGoU159V9S5uV2lpsAI0
the rails logs show a POST request coming through but don't show any parameters being posted:
Started POST "/registrations" for 10.66.210.70 at Thu Sep 09 17:48:06 -0700 2010
Processing by RegistrationsController#create as */*
Rendered registrations/new.html.erb within layouts/application (14.0ms)
Completed 200 OK in 24ms (Views: 14.6ms | ActiveRecord: 0.1ms)
whereas a POST request coming from a browser results in this log entry:
Started POST "/registrations" for 192.168.66.20 at Thu Sep 09 17:49:47 -0700 2010
Processing by RegistrationsController#create as HTML
Parameters: {"submit"=>"true", "authenticity_token"=>"AfNG0UoTbJXnxke2725efhYAoi3ogddMC7Uqu5mAui0=", "utf8"=>"\342\234\223", "registration"=>{"city"=>"", "address"=>"", "name"=>"", "zip"=>"", "optin"=>"0", "state"=>"", "email"=>""}}
Rendered registrations/new.html.erb within layouts/application (13.7ms)
Completed 200 OK in 24ms (Views: 14.3ms | ActiveRecord: 0.1ms)
and finally, this is what ab logs for the request:
---
POST /registrations HTTP/1.0
User-Agent: ApacheBench/2.0.40-dev
Host: oz01.zappos.net
Accept: */*
Content-length: 63
Content-type: text/plain
---
why is it not picking up the post data?
if the "post" file is not there then i get an error message saying it can't find the file so i know at very least it is finding the file...
Maybe you need the -T option as stated in man ab:-
ab -n 1 -p post -v 4 -T application/x-www-form-urlencoded "http://oz01.zappos.net/registrations"
I tested with Django and it seem that Django don't really care about the content type header (it displayed the POSTed content whether I used -T or not) but Rails maybe want it.
Old question, but for the sake of anyone else who searches SO for this, here's how I got it to work.
Make EXTRA sure your post file is properly URL encoded with no extra non-printing characters or anything at the end. The most error-free way is just create it with code. I used some python to create mine:
>>> import urllib
>>> outfile = open('post.data', 'w')
>>> params = ({ 'auth_token': 'somelongstringthatendswithanequalssign=' })
>>> encoded = urllib.urlencode(params)
>>> outfile.write(encoded)
>>> outfile.close()
Example output:
auth_token=somelongstringthatendswithanequalssign%3D

Resources