I am trying to download data from secure HTTP. I need to pass cookies in order to get past the login and download the data. I try:
curl --u "username" --d "https://url.datasite.login" --cookie-jar
auth.cookies
curl --cookie auth.cookies -O "https://url.datalocation.file"
However after running the first command I'm given:
curl: no URL specified!
I've tried rearranging the order, changing quotations, using <> for the username, setting the username and url as environmental variables, etc. Neither seem to do the trick. Have also changed the shell from tcsh to bsh and have tried using "wget" instead which retrieves a 400 Error.
Here's my commands for using cookies for curl:
# testing to see before login
curl http://localhost:3000/me
# undefined
# login
curl --cookie cookies.txt --cookie-jar cookies.txt \
-X POST -H 'Content-type: application/json' \
--data '{"username":"blahblahblah"}' \
http://localhost:3000/login
# OR--
# curl --cookie cookies.txt --cookie-jar cookies.txt \
# -X POST -H 'Content-type: application/x-www-urlencoded' \
# --data 'username=blahblahblah' \
# http://localhost:3000/login
# verify that we are logged in
curl --cookie cookies.txt http://localhost:3000/me
# blahblahblah
# download the file, (usually curl http://thing/file > file)
curl --cookie cookies.txt --cookie-jar cookies.txt \
http://localhost:3000/download
# helloworldfile
and the sample application i developed to test it with:
var express = require('express');
var session = require('express-session');
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {}
}));
app.get('/login', (r, s) => s.send(`<html>
<body>
<form method="post">
<input type="text" name="username" />
<input type="submit" />
</form>
</body>
</html>`));
app.post('/login', (req, res) => {
var { username } = req.body
if (username && username.length && username.length > 5) {
req.session.login = username;
res.end();
} else {
res.end('bad login\n');
}
});
app.get('/me', (r, s) => s.send(r.session.login + '\n'));
app.get('/download', (req, res) => {
if (!req.session.login)
return res.send('bad login\n');
res.set({
'Content-Description': 'File Transfer',
'Content-Type': 'text/plain',
'Content-Disposition': 'attachment; filename="data.txt"',
'Content-Transfer-Encoding': 'binary',
'Expires': '0',
'Cache-Control': 'must-revalidate, post-check=0, pre-check=0',
'Pragma': 'public',
'Content-Length': '14'
});
res.end('helloworldfile');
});
app.listen(3000);
Related
I've the following dto:
export class CreatePersonDto {
#IsString()
#ApiProperty({
description: 'Person name',
example: 'Jhon Doe',
})
readonly name: string;
#ValidateIf((object, value) => value)
#IsString({ each: true })
#ApiProperty({
description: 'Clothes ids',
isArray: true,
type: String,
})
readonly clothes: string[];
}
This is the cURL generated by Swagger Ui:
(Unable to parse this in NestJs to a string array)
curl -X 'POST' \
'http://localhost:3000/person' \
-H 'accept: */*' \
-H 'Content-Type: multipart/form-data' \
-F 'name=Jhon Doe' \
-F 'clothes=id1,id2'
(Clothes are sent as a string)
The array form in the UI looks like this:
This is the expected cURL (Generated by postman, or manually):
(Nestjs automatically parse this to an array)
curl -X 'POST' \
'http://localhost:3000/person' \
-H 'accept: */*' \
-H 'Content-Type: multipart/form-data' \
-F 'name=Jhon Doe' \
-F 'clothes[0]=id1' \
-F 'clothes[1]=id2'
(Clothes are correctly send as an array)
How can i solve this problem with swagger?
Hi I am trying to create a payment module for my rails application with sum up. This is the rest api that they are providing I tried with RestClient but it is returing 400 bad request.
curl -X POST \
https://api.sumup.com/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials'\
-d 'client_id=**Client_ID**'\
-d 'client_secret=**Client_Secret**'
This is what my restclient method looks like :
RestClient::Request.execute(
method: :post,
url: "https://api.sumup.com/token",
data: "grant_type=client_credentials&client_id=**CLIENT_ID**&client_secret=**Client_Secret**",
headers: { "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencode" }
)
Am I doing something wrong ?
You don't need to manually form encode the parameters which is a very likely source of errors.
RestClient.post(
"https://api.sumup.com/token",
{
grant_type: "client_credentials"
client_id: "**CLIENT_ID**"
client_secret: "**Client_Secret**"
},
{
accept: "application/json",
content_type: "application/x-www-form-urlencode"
}
)
I have this curl command which I need to covert to PUT request
curl https://example.com/api/v2/students/id.json \
-d '{"student":{"description":{"body":"Adding a new test description"}}}' \
-H "Content-Type: application/json" \
-v -u test#gmail.com:Abcd1234 \
-X PUT
Trial
I tried this PUT, but it doesn't work. It doesn't throw any error, but it does not add the description.
put(
"https://example.com/api/v2/students/id.json",
{:student => {:description => {:body => 'Adding a new test description.'}}},
{ 'Authorization' => "Basic #{authorization_token}" }
)
In your curl example, you provided the body as a (JSON-formatted) string:
curl ... \
-d '{"student":{"description":{"body":"Adding a new test description"}}}' \
...
The direct equivalent in rest-client would also use a (JSON-formatted) string:
put( ...,
'{"student":{"description":{"body":"Adding a new test description"}}}',
...
)
According to the README:
rest-client does not speak JSON natively, so serialize your payload to a string before passing it to rest-client.
You can use the rest-client log to show the actual HTTP request sent, and compare it with what curl sends.
How to debug/display request sent using RestClient
How to display request headers with command line curl
curl https://example.com/api/v2/students/id.json \
-d '{"student":{"description":{"body":"Adding a new test description"}}}' \
-H "Content-Type: application/json" \
-v -u test#gmail.com:Abcd1234 \
-X PUT
use
put(
"https://test%40gmail.com:Abcd1234#example.com/api/v2/students/id.json",
{student: {description: {body: 'Adding a new test description.'}}},
#{'student': {'description': {'body': 'Adding a new test description.'}}},
#{:student => {:description => {:body => 'Adding a new test description.'}}}.to_json,
{content_type: :json, accept: :json}
)
I want to traduce this curl into rest client sintax:
curl https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/cards \
-u sk_e568c42a6c384b7ab02cd47d2e407cab: \
-H "Content-type: application/json" \
-X POST -d '{
"token_id":"tokgslwpdcrkhlgxqi9a",
"device_session_id":"8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o"
}'
The hash I already have it in a variable and the keys or id´s are static so I paste them wherever I need to. This is what I´ve done so far but it doesn't work:
response_hash=RestClient.post "https://sandbox-api.openpay.mx/v1/mdxnu1gfjwib8cmw1c7d/customers/#{current_user.customer_id}/cards \
-u sk_083fee2c29d94fad85d92c46cec26b5a:",
{params: request_hash},
content_type: :json, accept: :json
Can someone help me traduce it?
Try this:
begin
RestClient.post(
"https://sk_e568c42a6c384b7ab02cd47d2e407cab:#sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/cards",
{ token_id: 'tokgslwpdcrkhlgxqi9a', device_session_id: '8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o' }.to_json,
{ content_type: :json, accept: :json }
)
rescue RestClient::ExceptionWithResponse => e
# do something with e.response.body
end
I have a cURL call that works but when I translate it using the Ruby Gem rest-client I get:
RestClient::UnsupportedMediaType: 415 Unsupported Media Type
Here is the cURL I used that worked:
curl \
-X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer MY_TOKEN" \
-H "Amazon-Advertising-API-Scope: MY_SCOPE" \
-d '{"campaignType":"sponsoredProducts","reportDate":"20161013","metrics":"impressions,clicks,cost"}' \
https://advertising-api.amazon.com/v1/productAds/report
Here is the Ruby that returns the HTTP 415 status:
yesterday = Date.today - 1
RestClient::Request.execute(
method: :post,
url: 'https://advertising-api.amazon.com/v1/productAds/report',
headers:
{
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{ENV['AD_ACCESS_TOKEN']}",
'Amazon-Advertising-API-Scope' => ENV['AD_PROFILE_ID']
},
payload:
{
'campaignType' => 'sponsoredProducts',
'reportDate' => "#{yesterday.year}#{yesterday.month}#{yesterday.day}",
'metrics' => 'impressions,clicks,cost'
}
)
The payload hash needed to be converted to JSON.
...
payload:
{
...
}.to_json
...