A newbie question I assume but here we go: I have the following params:
{"utf8"=>"✓",
authenticity_token"=>".........",
"import"=>
{"csv"=>
#<ActionDispatch::Http::UploadedFile:0x007fb59092a660
#content_type="text/csv",
#headers="Content-Disposition: form-data; name=\"import[csv]\"; filename=\"Users.csv\"\r\nContent-Type: text/csv\r\n",
#original_filename="DemoUsers.csv",
#tempfile=#<File:/var/folders/_p/w29hlx3x0cs6h026txv_rqhc0000gn/T/RackMultipart20141211-8204-1ha0i1u>>,
"datatype"=>"users"},
"commit"=>"Import",
"action"=>"create",
"controller"=>"imports"}
In my code, I need to assigns the value of #tempfile to a local variable but I just cant figure out how. ;-)
Most part of params are in params. So try
local_val = params["import"]["csv"].tempfile
suppose you assign response to a variable res
res = {"utf8"=>"✓",
authenticity_token"=>".........",
"import"=>
{"csv"=>
#<ActionDispatch::Http::UploadedFile:0x007fb59092a660
#content_type="text/csv",
#headers="Content-Disposition: form-data; name=\"import[csv]\"; filename=\"Users.csv\"\r\nContent-Type: text/csv\r\n",
#original_filename="DemoUsers.csv",
#tempfile=#<File:/var/folders/_p/w29hlx3x0cs6h026txv_rqhc0000gn/T/RackMultipart20141211-8204-1ha0i1u>>,
"datatype"=>"users"},
"commit"=>"Import",
"action"=>"create",
"controller"=>"imports"}
Now,
res["import"]["csv"].tempfile
Related
With a folder contained around 10 files (like 1.csv, 2.csv....10.csv) And I am uploading them to my http request using a beanshell preprocessor using the following script,
File folder = new File("C:\\User\\SYSTEMTESTING\\SAMPLENEWFILES\\REUPLOADFILES");
File[] fileForUpload = folder.listFiles();
Random rnd = new Random();
vars.put("CURRENT_FILE", fileForUpload[rnd.nextInt(fileForUpload.length)].getAbsolutePath());
want to get the file name which is uploaded using a JSR223 post processer
log.info("File Uploaded Is :"+${CURRENT_FILE});
I am getting,
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script8.groovy: 1: Missing ')' # line 1, column 79.
"File Uploaded Is ------->"+C:\Users\SY
In the request body:
PUT data:
--RhvwJL7ZdnIMBIaE0CoKVhsE68UNUiH
Content-Disposition: form-data; name="file"; filename="CSV_10_MB.csv"
Content-Type: application/vnd.ms-excel
Content-Transfer-Encoding: binary
<actual file content, not shown here>
--RhvwJL7ZdnIMBIaE0CoKVhsE68UNUiH--
I want the CSV_10_MB.csv name.
Use vars.get:
log.info("File Uploaded Is :" + vars.get("CURRENT_FILE"));
If you want to use Groovy's GString you need to declare this CURRENT_FILE property, if this is what you're looking for you need to amend your code like:
def CURRENT_FILE = vars.get("CURRENT_FILE")
log.info("File Uploaded Is: ${CURRENT_FILE}")
or use the same vars shorthand for printing the filename to JMeter log like:
log.info("File Uploaded Is: " + vars.get("CURRENT_FILE"))
I am trying to upload multiple files with the following request along with stringed JSON object:
Content-Disposition: form-data; name="params"
{"data":{"userName":"jim","description":"test","email":"jim#ox.com"}}
-----------------------------5366762814869373672043632099
Content-Disposition: form-data; name="file0"
VBORw0KGgoAAAANSUhEUgAAAFwAAAA/CAYAAABtj6+sAAAYJ2lDQ1BJQ0MgUHJvZmlsZQAAWIWVeQdUFE2zds
..... (content of file0)
-----------------------------5366762814869373672043632099
Content-Disposition: form-data; name="file1"
cBORw0KGgoAAAANSUhEUgAAAEsAAABpCAYAAAByKt7XAAAYJ2lDQ1BJQ0MgUHJvZmlsZQAAWIWVeQdUFE2zds
..... (content of file1)
-----------------------------5366762814869373672043632099--
file0 is of 16KB and file1 is of 12KB
and My spring controller method looks like this:
#POST
#Path("/addFile")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public void addFilesWithParams(MultipartFormDataInput filesData)
{
//some logic
}
But always ends up with one file saved and second file is ignored.
So i went ahead debuged my code and found out that On REST side I am only getting 2 Parts of the form i.e params and file0 and file1 is totaly ignored.
As you can see in the above snap it says size is 2, even if 3 or 4 parts are being sent from ui.
So my guess is there is some Size cap that I have to override or specify.
Can anyone help me with this issue.
I'm experimenting with grape and Ruby by trying to make a Yo API callback function.
I can get simple examples up and running like this . . .
resource :loc do
get ':loc' do
params.to_yaml
end
end
How would I go about extracting username and x and y coordinates into separate ruby variable given a callback with the following format?
http://yourcallbackurl.com/yourendpoint?username=THEYOER&location=42.360091;-71.094159
When the location data is screwed up . . .
--- !ruby/hash:Hashie::Mash
username: sfsdfsdf
location: '42.360091'
"-71.094159":
route_info: !ruby/object:Grape::Route
options:
:prefix:
:version: v1
:namespace: "/loc"
:method: GET
:path: "/:version/loc/:loc(.:format)"
:params:
loc: ''
:compiled: !ruby/regexp /\A\/(?<version>v1)\/loc\/(?<loc>[^\/.?]+)(?:\.(?<format>[^\/.?]+))?\Z/
version: v1
loc: toto
format: txt
This is how Rack::Utils works. Default params separators are "&" and ";" (its totally legal according to HTTP standard). So you have to parse query string by yourself here.
location = Rack::Utils.parse_nested_query(env['QUERY_STRING'], '&')['location']
coordinates = location.split(';')
UPD: typo with hash key fixed.
I am trying to convert this request header into Ruby format:
curl http://example.com/api/v1/info -H 'Authorization: Token token="asklasjqwAiSo1s2dj5ias23dkl"'
I am trying to add it to an HTTP GET request:
http = Net::HTTP.new(endpoint, 80)
http.get(path, authorization_header_with_token)
How would I build the header I used in the cURL request to work with the Ruby request?
The header hash parameter should look like this:
http.get(path, {'Authorization' => 'Token token="asklasjqwAiSo1s2dj5ias23dkl"'})
i have reduced this to as little as possible but wondering what i am doing wrong with this little ruby file:
params ={'title'=>'qwert', 'fulltext'=>'qwert', 'user_twitter_id'=>'qwert'}
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3000/details/'), params)
puts res.body
This does result in a record being created but none of the params being inserted, yet they seem recognised?
Started POST "/details/" for 127.0.0.1 at 2011-10-31 12:37:02 +0000
Processing by DetailsController#create as
Parameters: {"title"=>"qwert", "fulltext"=>"qwert", "user_twitter_id"=>"qwert"}
AREL (0.3ms) INSERT INTO "details" ("title", "fulltext", "user_twitter_id", "created_at", "updated_at") VALUES (NULL, NULL, NULL, '2011-10-31 12:37:02.401881', '2011- 10-31 12:37:02.401881')
Redirected to http://127.0.0.1:3000/details/23
Completed 302 Found in 185ms
In standard Rails you need a root element (you don't show the controller action, so I must assume):
params = {:detail => {...}}
BTW, a higher-level library as rest-client may come handy ([edit] Marian notes that nested hashes are not managed by Net::HTTP, so try rest-client)