How to pass form-data with rest-client - ruby

I am trying to translate a post request I send to a server with Postman to ruby RestClient.
In Postman, I send a (successful) POST request with a URL with params.
In my headers I have one parameter
Under Body I send more parameters under form-data. I tried adding these to the payload, which does not work:
I tried to translate it to the following call with rest-client
url = "https://..."
cookie = "..."
payload = {
payloadParam: "some param",
formDataParam: "form-data param"
}
headers = {
Cookie: cookie
}
Following their docs I then fired the following call:
data = RestClient.post(url, payload, headers)
So this doesn't pass the form-data params correctly. I have a feeling I am supposed to pass these parameters through something other than the payload, but I can't find any docs online explaining how. All I could find is a multipart example of loading an external file, but I don't have an external file.
How do I add form-data params to a POST request with Rest-Client?
My cURL structure looks like this:
curl -X POST \
'https://.../?payloadParam=xxxxxxxx' \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 55' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: xxxxxxxx' \
-H 'Host: xxxxxxxx.com' \
-H 'Postman-Token: xxxxxxxxxxxxxxx' \
-H 'User-Agent: PostmanRuntime/7.19.0' \
-H 'cache-control: no-cache' \
-d 'formDataParam=xxxxx&secondFormDataParam=yyyyy'

Related

Laravel 9 Request Always Blank When Update Data

I am using laravel 9 and Passport, I can't update data neither PUT nor PATCH method is working.
Here is my route api.php
Route::group(['middleware' => 'auth:api'], function() {
Route::get('logout', [AuthController::class,'logout']);
Route::resource('user',UserController::class);
Route::resource('/admin/post', PostController::class);
});
all another protected routes is ok, but when i Put or patch to localhost:8000 the request is always blank even the id is correct, for example in localhost:8000/api/admin/post/1 and in PostController update(Request $request,$id) when i dd($request, $id) will be "null",5
my request
curl --request PATCH \
--url http://localhost:8000/api/admin/post/5 \
--header 'Accept: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiZDU3MjhjZjZlMGVjMTVmMmQ1ZTJhYzFkMDU3MGQ0YzdhMDBlZmFhMDBjNWY2NDI1Yjk5MjY5YTFiZDNjNDE3Y2MxMmJmMjcwY2FkMjI0ZWUiLCJpYXQiOjE2NjA1MDMxNzYuNTUxMDk2LCJuYmYiOjE2NjA1MDMxNzYuNTUxMDk5LCJleHAiOjE2OTIwMzkxNzYuNTI5MTI0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.VvYis4iH3pUNLp1Zz4UOC9lQ6h4Gpi4xA44BA4rd7iQe-Z5IowjEwMkkYceuq8sZJAnbp9xh-li_zYXq-tEFakN9gKm3-hxr6gE8sqRj4WCSnVOu_KoqSKWX_FVlDW_IEhIfzGQdeIIRDgXuyz5E6dXxpqqtEsOiRY0KNKxjKRIG7gz0D0CO4lzTRkbly8nCG-CeBBYYS_jXonG4comxPk6eWnbDkw4yNBtdHQ9HK-E92PixbeMW13JPZ5Iiu-JmhLfOSSLrUqa_bJNW7WmbPTsBiyNZyRZ8kEYVbeVvKpMl37HoIDvJnvS9iydLW20Zb7KQkFEoI7bFS7jJdySXXPWIHqnqnFv4u0P6ko1rlLt5PCzyitIX_TdxvXYSaxfUQJMf9SPCG2P0Juxhhcsym8ob70VskVIwSCIGgvbi96JNKBhIhR9bJUX6eEhEOWeWB7jBRaquevTtpTBAm2qbGJPv4Crx2DksFHIuBOxCD33F-MFOSxvK5K2t-NTOofic2cOziYLSgVSrPYy2HHdnriWoAvZynUM2hDYKntzG8x3VYqswYkXwONtyQZpGAGBnDcZq9Fa2YDV6Poqwy_RdeCY6wlyF_1RI6unOgpDYm6GtTE4n51jijNx_kTm9s7F48CHva4aGNATQPoRYuVSWdOZo5VcgkKMfI04np4fQTeY' \
--header 'Content-Type: multipart/form-data' \
--form 'title=mau diganti' \
--form 'description=Sebuah tutorial untuk membunuh polisi' \
--form 'content=hehe' \
--form 'tags=[1,2]' \
--form 'categories=[1]' \
--form 'thumbnail=#C:\Users\Bloop\Pictures\anak kak risti.jpg' \
what is wrong?
Add _method=PUT or _method=PATCH in your body, and use curl --request POST instead.
The PUT and PATCH methods aren't allowed in HTML forms, so Laravel uses this workaround instead.
From the Laravel Docs:
HTML forms do not support PUT, PATCH, or DELETE actions. So, when defining PUT, PATCH, or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method
Chose POST method and add PUT or Patch to your request body

How to send json using faraday

I need to consume the linode api, in the description it says I have to send the data using curl like this
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X POST -d '{
"label": "asdfasdf"
"ssh_key": "asdfasdf"
}' \
https://api.linode.com/v4/profile/sshkeys
I tried using
http = Faraday::Connection.new 'https://api.linode.com/v4/profile/sshkeys'
http.authorization :Bearer, token
http.post('', { 'label' => 'adfadf', ..}.to_json)
But every request it says label and ssh_key required. I donĀ“t know how to send this particular request
anyone?
You need to specify the content-type as json in the header, if you're sending a json data.
http.post(
'',
{ 'label' => 'adfadf', ..}.to_json,
{ 'Content-Type' => 'application/json' }
)
Reference: https://lostisland.github.io/faraday/usage/

Sending a POST request with RestClient and Authentication headers

I have the following curl request that I would like to translate into a RestClient API request.
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Token user_token="tokenhere", email="email#myemail.com"' -X POST "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12" -v
I tried the following as a RestClient POST but I keep getting a 401. When I do the curl request I get a response.
url = "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12"
RestClient.post(url, :authorization => 'Token email="email#myemail.com" user_token="tokenhere"', :content_type => 'application/json', :accept => 'application/json')
Any ideas?
The string you're expecting is:
'Token user_token="tokenhere", email="email#myemail.com"'
The line you're sending is:
'Token email="email#myemail.com" user_token="tokenhere"'
The parameters are flipped around. :) If that doesn't work, I'd check and make sure that the curl request is expecting escaped characters. You could be effectively sending this:
"Token email=\"email#myemail.com\" user_token=\"tokenhere\""

How can I run this curl command in Ruby?

curl --request PROPFIND --url "http://carddav.mail.aol.com/home/testuser#aol.com/Contacts/" --header "Content-Type: text/xml" --header "Depth: 1" --data '<A:propfind xmlns:A="DAV:"><A:prop><A:getetag /><D:address-data xmlns:D="urn:ietf:params:xml:ns:carddav"/></A:prop></A:propfind>' --header "Authorization: Bearer fjskdlfjds"
Is there a particular method in Net::HTTP that allows the propfind command?
Net::HTTP::Propfind
Here is an example:
uri = URI.parse('http://example.com')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Propfind.new(uri.request_uri)
# Set your body (data)
request.body = "Here's the body."
# Set your headers: one header per line.
request["Content-Type"] = "application/json"
response = http.request(request)

Parse Cloud - make a GET endpoint

I have made simple cloud function:
Parse.Cloud.define("clientsnames", function(request, response) {
var query = new Parse.Query("Clients");
query.select('name')
query.find({
success: function(results) {
response.success(results)
},
error: function() {
response.error("Failed");
}
})
});
But to call it I always must use POST
curl -s -X POST \
-H "X-Parse-Application-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "X-Parse-REST-API-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Content-Type: application/json" \
-d "{}" \
https://api.parse.com/1/functions/clients
Which is, of course, a bad idea, and the request itself is obviously should be GET. But when I use GET with this request, I just get 404 (Document not found).
Is it possible to define a GET endpoint using Parse Cloud?
You can use Express js hosted on Parse CloudCode to setup custom endpoints
That way you can configure GET, POST without even using the Parse keys in the headers.
Head over to
https://www.parse.com/docs/cloud_code_guide#webhooks for detailed example

Resources