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?
Related
I want to create RSpec case for the post-API. in which data (JSON format + pdf files) are passing as form. e.g
curl --location --request POST 'http://localhost:3000/api/v1/books' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer eyJra...' \
--form 'book="{ \"book\": {
\"info\":{
\"reference_id\": \"43999\"
}
}
}"' \
--form 'book_content=#"/home/bookcontent.pdf"' \
--form 'book_booksummery=#"/home/booksummery.pdf"'
similarly, I can also create from postman too. How can I create the same form data in rspec and attached files in params? I tried it but the controller is unable to receive params.
it 'updates inquiry' do
post '/api/v1/books', params: book="{ \"book\": {
\"info\":{
\"reference_id\": \"43999\"
}
}
}".merge(book_content: fixture_file_upload('spec/fixtures/files/book_content.pdf', 'application/pdf'))
.merge(book_booksummery: fixture_file_upload('spec/fixtures/files/book_booksummery.pdf', 'application/pdf'))
, headers: headers_data
Rails.logger.info response
expect(response).to have_http_status(201) # giving 422 status error code
expect(json['reference_id']).to eq '43999'
end
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 am using curl to post json to my example service. I am posting json to the service as shown below
curl \
> -i \
> -H "Accept: application/json" \
> -H "Content-type: application/json" \
> -X POST \
> -d '{"email":"micky#minn.com","full_names":"Inisne Dats", "password": "oasswn"}' http://localhost:3000/api/admin_users
Below is my method thats responsible for creating and saving the new Active record Object
def create
user = AdminUser.new(white_list)
if user.save
head 200
else
puts user.errors.full_messages
head 500
end
end
My Whitelist method below
private
def white_list
params.require(:admin_user).permit(:email,:full_names,:password)
end
The problem is that this code never saves because the password is FILTERED, I have inspected the parameters and this is the output;
{"email"=>"micky#minn.com", "full_names"=>"Inisne Dats", "password"=>"[FILTERED]", "admin_user"=>{"email"=>"micky#minn.com", "full_names"=>"Inisne Dats"}}
Can anyone help show me how to retrieve the filtered password and use it once the parameters reach the controller method create. Or I'm I using whitelisting feature wrong?
#j-dexx gave me this answer. I had to wrap the parameters with a root "admin_user" key like so:
{ "admin_user":
{ "email": "micky#minn.com",
"full_names": "Inisne Dats",
"password": "oasswn",
"password_confirmation": "oasswn"
}
}
...so I changed my post request to:
curl \
> -i \
> -H "Accept: application/json" \
> -H "Content-type: application/json" \
> -X POST \
> -d '{"admin_user":{"email":"micky#minn.com","full_names":"Inisne Dats","password":"oasswn","password_confirmation":"oasswn"}}' http://localhost:3000/api/admin_users
That did the trick!
I would like to convert following curl code to ruby.
curl -u "my_username":"my_pass" \
-X POST \
-F "positive_examples=#/Users/abc/Downloads/tiger.zip" \
-F "negative_examples=#/Users/abc/Downloads/leopard.zip" \
-F "name=tiger" \
"http://localhost/api/v2/class"
Finally, I could convert the curl example to Ruby, follow the example in ruby:
request = RestClient::Request.new(method: :post,
url: 'http://localhost/api/v2/class',
user: 'my_username',
password: 'my_pass',
payload: {multipart:true,
positive_examples:File.new("/Users/abc/Downloads/tiger.zip", 'rb'),
negative_examples:File.new("/Users/abc/Downloads/leopard.zip", 'rb')
name:'tiger'})
RestClient::Request.execute method: :post,
url: 'http://localhost/api/v2/class',
user: 'my_username',
password: 'my_pass',
payload: {
multipart: true,
positive_examples: File.new('/Users/abc/Downloads/tiger.zip', 'rb'),
negative_examples: File.new('/Users/abc/Downloads/leopard.zip', 'rb'),
name: 'tiger',
}
Simply read the gem's README.