How do I extract the response code from a JSON API request? - ruby

Doing this:
#resp = Net::HTTP.get_response("api.something.com", "/feed/v1/offers.json?#{#params_api_string}")
I get this response in #resp:
#<Net::HTTPOK:0x7f451e9d3ef0>
How can I extract that OK, or No Content, Bad Request, Unathorized and so on in one variable? Is there some Net::HTTP function to get just that information?

#resp.message
should give you the message.
More methods of the HTTPResponse are available in the documentation.

Related

How Locust can extract value from response then using for next sequence request

I'm looking for an alternative tool to Gatling and found Locust. I found it's powerful but I wonder how can I extract a Json value and then using that value in the Json payload of the next request (please note the Json payload is in different file). Like in Gatling we can use saveAs() and the value will be store in the session variable, then we can put that value in Json payload.
Thanks,
Hoang
Locust is pretty much just Python, using the requests http client, so look in to the requests documentation for more info.
Lets assume /login returns a session_id in the json response that we want to use in future requests. You would then do something like this in your task:
response = self.client.post("/login")
session_id = response.json()["session_id"]
self.client.post("/foo", json={"session_id": session_id})

http.post request not receiving any data

I have a program that sends an HTTP request and receive the answer.
http = Net::HTTP.new(server, port)
resp, data = http.post(path, url, headers)
After this I see resp with a value of #<Net::HTTPOK:0x00005643dd572980> and data empty, so I receive a
no implicit conversion of nil into String error afterwards when I use it.
I have never used Ruby so I'm a bit confused about what can be failing or how I can debug this. I capture network traffic and both the request and response are sent correctly, and the response contains the expected data.
Is there anything that can make data be empty? How can I debug this?
Per the documentation, the Net::HTTP post method returns an HTTPResponse object. Since there is only one object returned, only resp will be given a value.
You mentioned that "the response contains the expected data." I am wondering if you simply need to employ the body method. Does resp.body give you the data you are looking for?
See https://ruby-doc.org/stdlib-2.7.1/libdoc/net/http/rdoc/Net/HTTP.html#method-i-post

Pass parameters in RestClient post request

I want to send parameters with a POST request using the RestClient gem (but I don't want to pass the params as JSON).
In a GET request, I would have:
url = "http://example.com?param1=foo&param2=bar"
RestClient.get url
How can I pass the same params but in a POST request without passing a JSON ?
Read ReadMe file of Rest-client git, it has lots of examples showing different types of request.
For your answer, try :
url = "http://example.com"
RestClient.post url,:param1=>foo, :param2=>bar

Firefox HTTP resource test post parameter

I'm using HTTP Resource Test on Firefox and want to simulate a POST request. However I didn't find where to write the post parameters. There are only URI, Representation and Headers, is there any way that I can pass with some post parameter? (In Json format?)
The payload of the post should go in the body of the request. It's shown here:
https://addons.cdn.mozilla.net/img/uploads/previews/full/54/54035.png?modified=1297743921
For example, if you were posting a JSON document you could just paste the JSON in the "Body" text area.
The payload of the post should go in 'Client Request' / 'Representation'.
It's shown on this screenshot.
As an aside, this answer describes an excellent test server to use as a sanity-check.

HTTParty parsed_response returns a String instead of Hash

HTTParty's parsed_response method returns a Hash if you get a response code 200 but otherwise it will return a String regardless if the webserver returns a XML response.
HTTParty.get(post_url).parsed_response.class # Depends on response code
Amazon will provide XML (explaining what went wrong) even on a 403.
Am I missing something?
HTTParty parses its #parsed_response based on what the HTTP response's Content-Type header is. Verify what the value is for that HTTP header. In your case, you'd want it to be application/xml.
In case anyone is still encountering this issue today, get can take a format parameter, which can help you ensure your HTTParty::Response object is a Hash:
url = HTTParty.get('http://domain.com', format: :json) # class is a Hash

Resources