How to frame the rest assured script for a post request based on CURL - rest-assured

I need to convert the below curl command into rest-assured POST request.
Can someone help me out
curl -ks --http1.0 -X POST "$server?format=pdf&tid=TD1" --cookie "INTERSET_SESSION=$access_token" -H 'content-type: multipart/form-data' -F 'template=#../templates/dashboard.hbs' -o output.pdf
The parameter template=#../templates/dashboard.hbs holds a file.

Check if the below helps
{
RequestSpecification req = RestAssured.given();
req.header("Content-Type", "multipart/form-data");
req.header("Cookie", "INTERSET_SESSION=ABCD");
req.multiPart("file2", new File("template=#../templates/dashboard.hbs"));
Response resp = req.post("$server?format=pdf&tid=TD1");
}

Related

Creating an issue with JIRA API `Anonymous users do not have permission to create issues in this project. Please try logging in first.`

I have a simple request that I am sending to a JIRA server
curl \
-D- \
-u 'api_key_label:api_key' \
-X POST \
--data '{"fields": {"project":{"key": "my_proj"}, "issuetype": {"name": "Bug"}}}' \
-H "Content-Type: application/json" \
https://my_instance/rest/api/2/issue/
When I send the request I get the response
{"errorMessages":[],"errors":{"project":"Anonymous users do not have permission to create issues in this project. Please try logging in first."}}%
If anyone has any experience with this I would appreciate some advice. Is the -u parameter supposed to base64 encoded?
Python
import requests
from requests.auth import HTTPBasicAuth
import json
data = json.dumps({"fields": {"project":{"key": "VER"},"summary": "summary": {"name": "Bug"}}})
url = 'https://my_instance:8080/rest/api/2/issue/'
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url,auth=('email_accoount','api_key'),data=data,headers=headers)

ruby add data to POST Request body as Formdata

I am working on an implementation where I need to call a POST web-service which expects body as form-data. So when I test from PostMan app I select 'Body' then 'form-data' and enter my two keys and their values to make the call.
This can be done in Java using jersey-media-multipart library and then doing something like this MultiPart mp = new MultiPart();
FormDataBodyPart my_form_data1 = new FormDataBodyPart("my_key1", inputStream1,
inputType1);
mp.addBodyPart(my_form_data1) .
However from my first Ruby on Rails project unsure how this can be accomplished.
Any ideas?
The Ruby class Net::HTTP can post form data. Documentation is here https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html.
For example
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
puts res.body
If you are using PostMan you can copy the command as curl and recreate it with Ruby code. For example, if you receive the following curl command form PostMan
curl -X POST \
https://example.com/somthing/somthing/hay-now \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Postman-Token: cabfef34-17b1-43b5-bb95-ac7a9ec35bcb' \
-d test=testvalue``
You can recreate the curl command like so.
require 'net/http'
uri = URI('https://example.com/somthing/somthing/hay-now')
request = Net::HTTP::Post.new(uri)
request. content_type = 'application/x-www-form-urlencoded'
request.set_form_data('test' => 'testValue')
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.request(request)
end
Please note, this is just an example.

curl POST request empty in ZF2 Rest API

I am testing my ZF2 Rest Module that is running on localhost, by sending curl POST requests from the same box.
curl -i -X POST -H "Content-Type: Application/json" -d '{username":"xyz","password":"xyz"}' http://localhost/api/login
In the corresponding controller and action, I have tried returning the POST parameters, but an empty array is returned always
var_dump($this->getRequest()); // returns: array(0){}
var_dump($_POST); // returns: array(0){}
If I switch from POST to GET with
curl -i -G -H "Content-Type: Application/json" -d '{username":"xyz","password":"xyz"}' http://localhost/api/login
it actually seems to work
var_dump($_GET); // returns: array(1) {["{"username":"xyz","password":"xyz"}"]=>string(0) ""}
Why is the POST request failing to pass/extract the parameters?
PHP only populates $_POST for form urlencoded POST data. You've explicitly set the content type to JSON, so the PHP way to access this would be:
file_get_contents("php://input");
In ZF2, I believe you want:
$this->getRequest()->getContent();
and in practice, you'll probably want to run this through json_decode().

curl - request with json object and file to Rails API

I need post file and JSON object to API (Ruby On Rails) by curl in one request.
My request looks like this:
curl --data #file.pdf -H "Accept: application/json" -H "Content-Type: multipart/form-data" -X POST -d '{"document":{"name":"file name"}}' http://localhost:3000/api/documents
But Rails parse it bad. Params on the server:
Parameters: {"{\"document\":{\"name\":\"file name\"}}"=>nil}
Where is problem?
You should pass multipart/form-data (as you specify), not a JSON string. Try this:
-d 'document[name]=file name'
Result on my machine:
Parameters: {"document"=>{"name"=>"file name"}}

post file using ruby curl easy.http_put (data)

I have a task to send a file using HTTP PUT in ruby.
In Command prompt i am able to get success response using the following command.
curl -X PUT -vN https://myexample.com/blah/myaccount --insecure -H "Connection: keep-alive" -H "Content-Type: audio/wav" -H "Content-Transfer-Encoding: base64" -d #/myfile.wav
While trying to send the request using Curl::Easy in ruby. I get a error response code stating something wrong with audio.
c = Curl::Easy.new("https://myexample.com/blah/myaccount")
c.timeout = 30
c.ssl_verify_host = false
c.http_put ( "Content-Transfer-Encoding=base64;Connection=keep-alive;Content-Type=audio%2fwav;#{File.read(/myfile.wav)}")

Resources