Ned
Ned

Reputation: 236

Postman POST works properly; Ruby POST does not. How do I debug the difference?

I need to do a POST request for an API that takes a JSON payload that contains a nested stringified JSON object. I do a GET request to get the string, parse it (twice!) to get the hash, modify one parameter, then POST it back. When I post via Postman, it goes through as normal. When I post the same string via the Ruby Rest-Client gem, it seems that the POST goes through but there is an issue with how the payload is stringified, providing an unwanted result.

Unfortunately, I cannot refactor this admittedly poorly-designed back end. I can only work with what I have.

I have tried a dozen permutations of this code. Everything going into the post looks correct and identical to the Postman post.

Here is the Postman HTTP-ified, stripped of identifying information:

POST /example/home/api/savesettings HTTP/1.1
Host: dev.noneofyourbusiness.com
Cookie: identification-gui=f4k3-gu1-5tr1n9
Content-Type: application/json

{"settings":"{\"rows\":50,\"columns\":[\"Col1\",\"Col2\",\"Col3\",\"Col4\"],\"sort\":{\"column\":\"Col1\",\"direction\":1},\"filters\":{\"Status\":[\"Active\"]},\"widths\":{\"Id\":\"170px\",\"Description\":\"192px\"}}"}

Here is the Ruby code, meant to do the same thing:

response = RestClient.get(
        get_url,
        :accept => :json,
        :cookies => {cookie[:name] => cookie[:value]}
    )

hash = JSON.parse(JSON.parse(response))
hash['columns'] = "Col1;Col2;Col3;Col4"
payload = {:settings => hash.to_json}.to_json

RestClient.post(
        post_url,
        payload,
        :content_type => :json,
        :cookies => {cookie[:name] => cookie[:value]}
    )

The Postman request sets the columns correctly. The Ruby request seems to wipe all of the settings, leading me to believe that the JSON is not formatted correctly, but I'm not sure.

When debugging, the Postman body/Rest-Client payload look identical.

Upvotes: 0

Views: 236

Answers (1)

Ned
Ned

Reputation: 236

After a week of slamming my head against the desk, I realized that all I needed to do is add

.split(";")

to the end of my columns. Everything works now.

Upvotes: 1

Related Questions