Understanding the Implementation of the Curl Command Ruby

Asked 2 years ago, Updated 2 years ago, 132 views

I am trying to realize the curl contents below in ruby.
There are 400 errors and it doesn't work.

curl-F grant_type=refresh_token-Frefresh_token=<refresh token>client_id=<client id>-F client_secret=<client secret>-X POST https://test/token

How should I describe the -F option of curl in ruby?
Sorry for the inconvenience, but I look forward to hearing from you.

April 18, 2018
Dear cubick and nekketsuuuuu

Thank you for your comment.
I'm sorry, but I didn't list any code or errors that I tried to implement.
Your question was not good.

Below are the codes and errors.

■ Code

def updateAccessToken()
    json='"
    uri=URI.parse("http://test/token")
    Net::HTTP.start(uri.host,uri.port){|http|
      request=Net::HTTP::Post.new(uri.path)
      request.set_form_data('grant_type'=>'refresh_token')
      request.set_form_data('refresh_token'=>'dfafdasfdasdfadsfsa3')
      request.set_form_data('client_id'=>'2121fdasgadsraea')
      request.set_form_data('client_secret'=>'2121fdasgadsraea')
      json=http.request(request)
    }
    return json
end

■Error

Net::HTTPBadRequest 400 Bad Request readbody=true

Dear Kunif,

Thank you for your comment.
I also tried the website you introduced me to, but the error I mentioned above appeared, so
The result was the same.

April 23, 2018
Dear Kunif,

Thank you for your comment.
I would like to conduct a survey using the method of the link that you introduced.
Thank you for your repeated support.

ruby curl

2022-09-30 21:30

1 Answers

400 Bad Request is not what the server expects.

Curl's -F issues a Content-Type:multipart/form-data request, but Ruby's Net::HTTP::Post issues a Content-Type:application/x-www-form-urlocated request.

If Ruby wants to issue a Content-Type: multipart/form-data request, it is recommended to use set_form instead of set_form_data.

I think the next page will be helpful.
http://masamitsu-murase.blogspot.jp/2017/01/ruby-nethttp-multipart-post.html


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.