1

Trying to add header in the request

email = "[email protected]"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1&params2=value2"

uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri)
request['email'] = email
request['token'] = token

response = http.request(request)

render json: response.body

The results i have gotten back is {"error":"invalid request parameters"}

I am supposed to get back a list of data in json. I tried using postman to test if the url is working and passed the email and token inside header and i got back the data that i wanted. I am not sure where it went wrong with the code. Can anybody advise me which part did i do wrongly? Thanks a lot!

2
  • Try to use requestb.in to check the format of the request. It's possible you are sending malformed data or you are missing some header. Commented Sep 10, 2016 at 9:24
  • @SimoneCarletti i have look through it but i am not sure how to use it, can you guide me on how to use that to check the request i am sending? Commented Sep 10, 2016 at 9:45

1 Answer 1

1

Try this.

email = "[email protected]"
token = "abcdefghijk"
url = "http://www.somewebsite.com/request?params1=value1&params2=value2"

uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port).start
request = Net::HTTP::Get.new(uri.request_uri, {'email'=>email,'token'=>token})
response = http.request(request)
render json: response.body

OR

request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("email", email)
request.add_field("token", token)
response = http.request(request)
render json: response.body
Sign up to request clarification or add additional context in comments.

5 Comments

i have tried this and i still get ' {"error":"invalid request parameters"}`. i am not sure why though. Is there any way to check?
@Edwin - Try this request.add_field("email", email)
@ vishram0709 it still doesn't work for me. i think that somehow the header isn't added in the request, no matter what i try because it always returns me invalid request parameters.
@Edwin - Try running on rails console and you might fetch some error. In my PC both these methods worked.
it still gives the same result. I am not sure whats wrong also. I even tried using the ruby code provided by Postman and it still gives the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.